|
|
|
|
|
by axle_512
2899 days ago
|
|
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. People freak out about the parens. But I think what's the big deal? Functions are called with the paren in front of the function and the args space separated.
so f(x, y) in C like syntax becomes: (f x y) - Whoop dee do, not so hard. And since the syntax is extremely consistent, things that are math "operators" in other langs are just functions in lisp. For "(+ 1 2 3)"
+ is actually the name of the function, so we are invoking + function and 1 2 3 are the arguments. Returns 6. Once you know that, and that function args are evaluated before passed into the function, then you no longer have to worry about operator precedence and parenthesis to control order of operations. Once you get used to it, it's actually a lot simpler. (* 2 (+ 5 5)) => 20 |
|
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 +
macro form with DEFUN. The syntax for DEFUN is: 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:
This means that is a valid program.This is not a valid Lisp program, which Lisp will complain about:
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.