Hacker News new | past | comments | ask | show | jobs | submit login

> In lisp, nearly everything is written in the same form. I.e. everything either is a function call or looks like one. Once you know that, you pretty much know lisp syntax.

Actually not. Lisp has several types of forms. A function call is one. There are atleast two others: macro forms and special forms.

function form with +

  (+ 1 2)
macro form with DEFUN. The syntax for DEFUN is:

  defun function-name lambda-list [[declaration* | documentation]] form*
The function name the is either a symbol or a list of (setf name). The lambda-list has complex syntax with optional, aux, rest and keyword arguments, declaration has a complex declaration syntax, etc...

The third type of form would be built-in syntax. LET is an example. The syntax for LET is:

  let ({var | (var [init-form])}*) declaration* form* => result*
This means that

  (let ((a 1)
        b)
    (setf b a))
is a valid program.

This is not a valid Lisp program, which Lisp will complain about:

    * (let ((a 1 2)
           b)
      (setf b a))
  ; in: LET ((A 1 2) B)
  ;     (A 1 2)
  ;
  ; caught ERROR:
  ;   The LET binding spec (A 1 2) is malformed.

Reason: Lisp expects that bindings are either symbols, lists with a single symbol or pairs with a symbol and a value form. (a 1 2) thus is not valid.

Is Lisp syntax easy? Once you look deeper, it actually isn't. Only a core language with only function calls is relatively easy.

* Lisp writes programs on top of a data structure called s-expressions. S-expressions are easy, Lisp not.

* Lisp usually has the operator as the first element of a lisp -> uniform appearance.

* Lisp has some built-in syntax. Usually as little as possible.

* Lisp lets the developer add new syntax via macros -> zillions of macros and the user needs to learn a few patterns to understand Lisp code.




Note I said "or looks like one".

If you squint a bit, macros and built-ins are similar in structure to functions (they're all s expressions).

I agree with much of what you said, but I still maintain that lisp syntax is still relatively trivial compared to most languages.

I also don't think exposing newcomers to macros on a language that is homoiconic is terribly useful until they're already comfortable with the basic s-expression syntax.


> If you squint a bit, macros and built-ins are similar in structure to functions

Because they have a parenthesis in front and back and the operator as first element. Macros then implement complex code structures between those:

  (foo a b)
and then

  (let ((a 10)
        b)
    (declare (type (integer 0 100) b)
             (type number a))
    (declare (optimize (speed 3))
    (declare (special a b)
             (dynamic-extent a))
    (prog ()
      start
       (setf b (+ a 10))
       (go end)
      end)
    (the integer (+ a a b)))
Now remove the parentheses. Looks like a program in a typical language.

Macro forms have lots of internal structure. For an extreme example check the syntax definition of the LOOP macro. Two pages of EBNF syntax declaration. That there are an opening parentheses and a closing one does not suddenly remove the syntax:

   loop for i from 1 below 10 by 2
          and
        for j from 2 upto 50 by 3
        when foo(i, j)
          collect i into is and j into js and i * j into ps
        when bar(i) > baz (j)
          return list(is, js, ps)
or the Lisp version:

  (loop for i from 1 below 10 by 2
          and
        for j from 2 upto 50 by 3
        when (foo i j)
          collect i into is and j into js and (* i j) into ps
        when (> (bar i) (> baz j))
          return (list is js ps))
Does it LOOK like it has less syntax? Not really.

> lisp syntax is still relatively trivial compared to most languages

Not really. Check out the concept of a code walker in Lisp. That's a tool which understands Lisp syntax and can walk over Lisp code and do transformations.

Here is one:

https://gitlab.common-lisp.net/cl-walker/cl-walker/tree/mast...

Mildly complex... complexity comes in, because Lisp has built-in syntax transformations via macros.


Fair enough, I see your point.

Though stylistically my personal preference is for the macro to remain as simple as possible with limited syntax introduced.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: