Re: Lisp syntax vs. Mathematica syntax



josephoswaldgg@xxxxxxxxxxx wrote:
> My personal "favorite" is Apply, where I had not defined a function
> "g". Check out Mathematica's documentation
>
> Apply[Plus, g[a,b]] --> a+b
>
> WTF! Hey, what happened to my function g? And, amazingly enough, at
> least some folks at Wolfram think this is what Lisp does,

Let's trying writing a Lisp macro equivalent to Mathematica's Apply.

In Mathematica, Apply may be defined as:

Apply[h_, _[args___]] := h[args]

For example:

In[1]:= Apply[Plus, g[3]]

evaluates Plus[3] to give:

Out[1]= 3

Effectively, Mathematica's Apply replaces a call to one function (_) with a
call to another (h).

Here's my puny attempt at a Lisp conversion that only works for
single-argument functions:

(defmacro mapply (`f `(g arg))
`(,f ,arg))

Repeating the same example, Lisp gives a few warnings but it seems to work:

* (mapply `+ `(g 3))

; In: LAMBDA (#:WHOLE-1516 #:ENV-1517)

; (LET* (# # # # #)
; `(,F ,ARG))
; Note: Variable G defined but never used.
;
; Note: Variable QUOTE defined but never used.
;
3

> and they'll start explaining to you about the "head" of an expression....
>
> You can also write a Mathematica-like syntax parser in Lisp. Look up
> Fateman's "mma" If you want to.

I've already looked at it.

>> Just to clarify, many of the symbols that you've listed are simply infix
>> functions. You've also omitted a lot of Mathematica's syntax (which is
>> unusually complicated).
>
> And some of those "functions" affect the pattern *transformations* of
> the expressions they define. That's not what Lisp folks mean when they
> talk about functional programming.

If you mean the difference between Rule and RuleDelayed, for example, then I
believe that is probably just shorthand for quoting and not quoting in a
Lisp macro.

>> Mathematica can also be very concise. For example, the following squares
>> each element in a list "l":
>>
>> #^2&/@l
>
> Yes, because Mathematica has the "feature" that it automatically
> distributes functions over lists.

No. If you want a function to be threaded over its arguments when they are
lists then you must set the Listable attribute for that function. There are
many possible attributes. This is one piece of functionality that makes
Mathematica's pattern matching much more powerful than most other forms of
pattern matching. However, Mathematica's pattern matching can be far from
linear.

In that example, I used the map function which can be written "func /@ list"
in Mathematica's infix notation. The same infix notation can be defined in
OCaml or SML, for example:

# let ( /@ ) f l = List.map f l;;
val ( /@ ) : ('a -> 'b) -> 'a list -> 'b list = <fun>

> What you are doing is not the same as other programming environments.

Yes. Equivalently, what Lisp macros do is "not the same as other programming
environments".

> Yes, you can write programs in Mathematica very easily in a huge number
> of various approaches. But will they come when you call them? Can you
> really be sure what they do? That you can avoid name capture? That
> things don't depend on the exact structure of the expressions you pass
> in? That you really made the right choice between Hold and Unevaluated?

I wrote most of my PhD in Mathematica and it worked superbly. Mathematica
also has many happy users. Indeed, my old department are apparently looking
into a site license. From my point of view, the main source of bugs in
Mathematica code is due to the lack of static type checking.

--
Dr Jon D Harrop, Flying Frog Consultancy
http://www.ffconsultancy.com
.



Relevant Pages

  • Re: why do you choose LISP?
    ... i like lisp primarily bceause it is a functional lang. ... It is also during the early 1990s, i started to learn programing on my ... during these years i bought Mathematica (because i heard it's the ... I do not have any concrete idea what IS a language specification ...
    (comp.lang.lisp)
  • Re: Lisp and Scheme with fewer parentheses / Mathematica??
    ... Any dummy, at our level, knew that Mathematica and lisp have different ... programer, even with say 10 years of programing experience, chances ... functional language and one of the oldest language) ...
    (comp.lang.lisp)
  • Re: Whats so great about lisp?
    ... >>into some representation that happens to be exact in Lisp. ... >>tracks numerical errors in floating point arithmetic: ... The representation used by Mathematica tracks numerical error, ... > Does Mathematica have built-in condition system like Common Lisp? ...
    (comp.lang.lisp)
  • Re: Benefits of Dynamic Typing
    ... Mathematica implements everything that you've listed from Lisp and adds ... > syntax for for representing data externally predefined ... Mathematica takes this further by allowing 2D typesetting of expressions. ...
    (comp.lang.functional)
  • Re: Lisp syntax vs. Mathematica syntax
    ... > You can write a term-level interpreter for Mathematica just as you can write ... You can also write a Mathematica-like syntax parser in Lisp. ... talk about functional programming. ...
    (comp.lang.lisp)