Skip to content

Some syntax examples

JackeLee edited this page Sep 13, 2010 · 14 revisions

Built in functions and constants:

constants: NIL, TRUE, FALSE
clasic arithmetic: +, -, *, /
logical functions: if, and, or, not, nil?, cons?, number?, char?, bool?, func?
number comparations: =, >
list operations: append, tail, head
other: list, cons, cdr, car, dump, print-string, env, apply

Note

  • NIL is the same as FALSE

Features

-- function inside function
[def reduce [f l]
        [def inside-reduce [f first l]
                [if [nil? l]  
                        first
                        [inside-reduce f [f first [head l]] [tail l]]
                ]
        ]

        [inside-reduce f [head l] [tail l]]
]

-- closures
[def foo [bar]
        [def foo2 [bar2] [+ bar bar2]]
        [foo2]
]

-- lambda functions
[reduce [lambda [x y] [* x y]] [list 1 2 3 4]]

-- unlimited number of params
[def foo [&bar]
        [head [tail &bar]]
]

[foo 1 2 3 4 5] => 2

-- Strings
"Lisp" => [list 'L' 'i' 's' 'p']

Examples:

-- n-th factorial
[def fact [n]
        [if [> n 1]
                [* [fact [- n 1]] n]
                1
        ]
]

-- simple reverse list
[def reverse [l]
        [if [nil? l]
                l
                [append [reverse [tail l]] [head l]]
        ]
]

-- foldL
[def reduce [f first l]
        [if [nil? l]  
                first
                [reduce f [f first [head l]] [tail l]]
        ]
]

-- classic map
[def map [f l]
        [if [nil? l]
                l 
                [append [f [head l]] [map f [tail l]]]
        ]
]
Clone this wiki locally