@@ -12,6 +12,10 @@ characteristics. There are Lisp-1 and Lisp-2 dialects and there is a difference
lexical scoping as opposed to dynamic scoping. These differences will be explained in later
lexical scoping as opposed to dynamic scoping. These differences will be explained in later
sections.
sections.
Like most Lisps, Slime is dynamically typed. That means that like in statically typed Languages
Slime has different data types, but they are associated not with variables but with the Lisp objects
themselves. Variables can be assigend Lisp objects of any internal type.
The Lisp language family is known to be highly flexible and applicable in all areas by creating
The Lisp language family is known to be highly flexible and applicable in all areas by creating
domain specific languages in Lisp itself through a powerful macro system. The central data structure
domain specific languages in Lisp itself through a powerful macro system. The central data structure
in Lisp is the list. The reason why lisp is so powerful is because the program source code itself is
in Lisp is the list. The reason why lisp is so powerful is because the program source code itself is
@@ -96,7 +100,7 @@ symbol). If the parser encounters a =.= inside of a list, it will treat the
=rest=. If there is no or more than one element after the =.= an parsing error will be thrown. Using
=rest=. If there is no or more than one element after the =.= an parsing error will be thrown. Using
this syntax we can represent the ill formed list from [[illFormedList]] as =(1 2 . 3)=. We can also
this syntax we can represent the ill formed list from [[illFormedList]] as =(1 2 . 3)=. We can also
write well formed lists using the dot notation if we point the rest to another list. So the well
write well formed lists using the dot notation if we point the rest to another list. So the well
formed list from [[simpleBoxDiagram]] can also be written as =(1 . (2 . (3)))=
formed list from [[simpleBoxDiagram]] can also be written as \[\texttt{(1 . (2 . (3)))}\]
** representing function calls in Lisp
** representing function calls in Lisp
@@ -195,7 +199,7 @@ value and only =I knew it!!= will be printed.
: I knew it!!
: I knew it!!
The programmer can also define their own special forms using =special-lambda= and macros, which will
The programmer can also define their own special forms using =special-lambda= and macros, which will
be explained later .
be explained in [[Special lambdas]] and [[Macros]] .
* Symbols and keywords
* Symbols and keywords
* Truthyness
* Truthyness
@@ -346,7 +350,10 @@ defined and supplied after all the regular arguments.
#+name: code:keyword-args
#+name: code:keyword-args
#+caption: A more complex functoin definition using keyword arguments
#+caption: A more complex functoin definition using keyword arguments
#+begin_src slime
#+begin_src slime
(define (complex required1 required2 :keys key1 key2 :defaults-to 3 key3)
(define (complex required1 required2 :keys
key1
key2 :defaults-to 3
key3)
(* (+ required1 required2)
(* (+ required1 required2)
key1
key1
key2
key2
@@ -382,7 +389,9 @@ other argument types, regular arguments and keyword arguments.
(when do-logging
(when do-logging
(printf "Executing operation"
(printf "Executing operation"
operation
operation
"agains values yielded:"
"agains the values"
values
"yielded:"
result))
result))
result)
result)
@@ -395,11 +404,11 @@ other argument types, regular arguments and keyword arguments.
#+RESULTS: code:rest-args
#+RESULTS: code:rest-args
: evaluates to =>
: evaluates to =>
: 6
: 6
: Executing operation [C-function] agains values yielded: 110
: Executing operation * agains values (10 11) yielded: 110
: 110
: 110
* Environments
* Environments
* Macros
* Built-in functions
* Built-in functions
This section provides a comprehensive list of the built in functions for Slime. Some of them are
This section provides a comprehensive list of the built in functions for Slime. Some of them are
defined in =C++= source code, some are themselves written in Slime. The cool thing about Slime is
defined in =C++= source code, some are themselves written in Slime. The cool thing about Slime is
@@ -410,25 +419,195 @@ embedded scripting language.
** Arithmetic functions
** Arithmetic functions
- =+= :: (=regular function [C++]=) Takes 0 or more numbers as arguments and returns the sum of all
- =+= :: (=regular function [C++]=) Takes 0 or more numbers as arguments and returns the sum of all
the numbers.
the numbers.
{{{slime_header}}}
#+name: code:built-in-=
#+begin_src slime
(printf (+))
(printf (+ 3))
(printf (+ 1 3 2))
(printf (+ 1 (+ 3 4)))
#+end_src
#+results: code:built-in-=
: evaluates to =>
: 0
: 3
: 6
: 8
- =-= :: (=regular function [C++]=) Takes 0 or more numbers as arguments. If only one number is
- =-= :: (=regular function [C++]=) Takes 0 or more numbers as arguments. If only one number is
supplied, its negation is returned, otherwise the difference of the first argument and the
supplied, its negation is returned, otherwise the difference of the first argument and the
sum of the remaining arguments is returned:
sum of the remaining arguments is returned:
\[\texttt{(- 10 2 1)} \Rightarrow 10 - 2 - 1 = 10 - (2 + 1) = 7\]
\[\texttt{(- 10 2 1)} \Rightarrow 10 - 2 - 1 = 10 - (2 + 1) = 7\]
{{{slime_header}}}
#+name: code:built-in--
#+begin_src slime
(printf (-))
(printf (- 3))
(printf (- 5 3 1))
(printf (- 5 (+ 3 1)))
#+end_src
#+RESULTS: code:built-in--
: evaluates to =>
: 0
: -3
: 1
: 1
- =*= :: (=regular function [C++]=) Takes 0 or more numbers as arguments and returns the product of
- =*= :: (=regular function [C++]=) Takes 0 or more numbers as arguments and returns the product of
all the numbers.
all the numbers.
{{{slime_header}}}
#+name: code:built-in-*
#+begin_src slime
(printf (*))
(printf (* 2))
(printf (* 5 3 2))
(printf (* 2 (+ 3 1)))
#+end_src
#+RESULTS: code:built-in-*
: evaluates to =>
: 1
: 2
: 30
: 8
- =/= :: (=regular function [C++]=) Takes 0 or more numbers as arguments. If only one number is
- =/= :: (=regular function [C++]=) Takes 0 or more numbers as arguments. If only one number is
supplied, it is returned, otherwise the quotient of the first argument and the product of
supplied, it is returned, otherwise the quotient of the first argument and the product of
the remaining arguments is returned:
the remaining arguments is returned:
\[\texttt{(/ 100 2 5)} \Rightarrow \frac{100}{\frac{2}{5}} = \frac{100}{2 \cdot 5} = 10\]
\[\texttt{(/ 100 2 5)} \Rightarrow \frac{100}{\frac{2}{5}} = \frac{100}{2 \cdot 5} = 10\]
{{{slime_header}}}
#+name: code:built-in-/
#+begin_src slime
(printf (/))
(printf (/ 3))
(printf (/ 1 2))
(printf (/ 2 (+ 3 2 1)))
#+end_src
#+RESULTS: code:built-in-/
: evaluates to =>
: 1
: 3
: 0.500000
: 0.333333
- =**= :: (=regular function [C++]=) Takes 2 number arguments and returns the the first argument
- =**= :: (=regular function [C++]=) Takes 2 number arguments and returns the the first argument
taken to the power of the second argument.
taken to the power of the second argument.
{{{slime_header}}}
#+name: code:built-in-**
#+begin_src slime
(printf (** 1 200))
(printf (** 2 6))
(printf (** 25 0.5))
(printf (** 27 (/ 1 3)))
#+end_src
#+RESULTS: code:built-in-**
: evaluates to =>
: 1
: 64
: 5
: 3
- =%= :: (=regular function [C++]=) Takes 2 number arguments and rounds them down to integer values
- =%= :: (=regular function [C++]=) Takes 2 number arguments and rounds them down to integer values
and then returns the remainder of the division of the first argument by the second.
and then returns the remainder of the division of the first argument by the second.
{{{slime_header}}}
#+name: code:built-in-mod
#+begin_src slime
(printf (% 10 3))
(printf (% (+ 3 (* 12 15)) 15))
#+end_src
#+RESULTS: code:built-in-%
: evaluates to =>
: 1
: 3
- =not= :: (=regular function [C++]=)
- =not= :: (=regular function [C++]=)
{{{slime_header}}}
#+name: code:built-in-not
#+begin_src slime
(printf (not 10))
(printf (not ()))
(printf (not (> 10 1)))
(printf (not (< 10 1)))
#+end_src
#+RESULTS: code:built-in-not
: evaluates to =>
: ()
: t
: ()
: t
- =and= :: (=regular function [C++]=)
- =and= :: (=regular function [C++]=)
{{{slime_header}}}
#+name: code:built-in-and
#+begin_src slime
(printf (and))
(printf (and 1 2 3 4))
(printf (and 1 2 () 4))
(printf (and (> 3 1) (< 3 10)))
#+end_src
#+RESULTS: code:built-in-and
: evaluates to =>
: t
: t
: ()
: t
- =or= :: (=regular function [C++]=)
- =or= :: (=regular function [C++]=)
{{{slime_header}}}
#+name: code:built-in-org
#+begin_src slime
(printf (or))
(printf (or 1 2 3 4))
(printf (or 1 2 () 4))
(printf (or (> 1 3) (< 3 10)))
#+end_src
#+RESULTS: code:built-in-org
: evaluates to =>
: ()
: t
: t
: t
- =increment= :: (=regular function [Slime]=)
- =increment= :: (=regular function [Slime]=)
{{{slime_header}}}
#+name: code:built-in-increment
#+begin_src slime
(printf (increment 11))
#+end_src
#+RESULTS: code:built-in-increment
: evaluates to =>
: 12
- =decrement= :: (=regular function [Slime]=)
- =decrement= :: (=regular function [Slime]=)
{{{slime_header}}}
#+name: code:built-in-decrement
#+begin_src slime
(printf (decrement 12))
#+end_src
#+RESULTS: code:built-in-decrement
: evaluates to =>
: 11
** Comparison functions
** Comparison functions
- === :: (=regular function [C++]=) Takes 0 or more arguments and returns =t= iff
- === :: (=regular function [C++]=) Takes 0 or more arguments and returns =t= iff
@@ -472,25 +651,232 @@ embedded scripting language.
\indent and =()= otherwise.
\indent and =()= otherwise.
** Controlflow
** Controlflow
+ =if= :: (=special form [C++]=)
+ =cond= :: (=special form [Slime]=)
+ =while= :: (=special form [C++]=)
+ =n-times= :: (=special form [Slime]=)
- =if= :: (=special form [C++]=) Takes 2 or more arguments. If the first argument (the
condition) evaluates to a truthy value, the second argument is evaluated and returned.
Else if more arguemnts are supplied, they will be evaluated and the last result will
be returned, if the condition was falsy and no further arguments were supplied, then
nil will be returned.
+ =when= :: (=special form [Slime]=)
+ =unless= :: (=special form [Slime]=)
{{{slime_header}}}
#+name: built-in-if
#+begin_src slime
(printf (if 1 1 2))
(printf (if () 1 2))
(printf (if () 1 ))
#+end_src
#+RESULTS: built-in-if
: evaluates to =>
: 1
: 2
: ()
- =cond= :: (=special form [Slime]=)
{{{slime_header}}}
#+name: built-in-cond
#+begin_src slime
(define (fib n)
(cond ((<= n 0) 0)
((= n 1) 1)
(else (+ (fib (- n 1))
(fib (- n 2))))))
(printf (fib 6))
#+end_src
#+RESULTS: built-in-cond
: evaluates to =>
: 8
- =while= :: (=special form [C++]=)
{{{slime_header}}}
#+name: built-in-while
#+begin_src slime
(define animals '("Bird" "Dolphin" "Giraffe"))
(while animals
(printf (first animals) "is an animal")
(define animals (rest animals))
)
#+end_src
#+RESULTS: built-in-while
: evaluates to =>
: Bird is an animal
: Dolphin is an animal
: Giraffe is an animal
- =n-times= :: (=special form [Slime]=)
{{{slime_header}}}
#+name: built-in-n-times
#+begin_src slime
(n-times 3 (printf "Three time's a charm"))
#+end_src
#+RESULTS: built-in-n-times
: evaluates to =>
: Three time's a charm
: Three time's a charm
: Three time's a charm
- =when= :: (=special form [Slime]=)
{{{slime_header}}}
#+name: built-in-when
#+begin_src slime
(printf (when 1 2 3))
(printf (when () 2 3))
#+end_src
#+RESULTS: built-in-when
: evaluates to =>
: 3
: ()
- =unless= :: (=special form [Slime]=)
{{{slime_header}}}
#+name: built-in-unless
#+begin_src slime
(printf (unless 1 2 3))
(printf (unless () 2 3))
#+end_src
#+RESULTS: built-in-unless
: evaluates to =>
: ()
: 3
** Functions for lists
** Functions for lists
- =pair= :: (=regular function [C++]=)
- =first= :: (=regular function [C++]=)
- =rest= :: (=regular function [C++]=)
- =list= :: (=regular function [C++]=)
- =end= :: (=regular function [Slime]=)
- =last= :: (=regular function [Slime]=)
- =extend= :: (=regular function [Slime]=)
- =pair= :: (=regular function [C++]=) Takes 2 arguments of any type and return a pair which
=first= field points to the first argument and the =rest= field points to the second
argument.
{{{slime_header}}}
#+name: built-in-pair
#+begin_src slime
(printf (pair 1 "yes"))
(printf (pair '+ ()))
(printf (pair '+ (pair 1 (pair 3 ()))))
(printf (eval (pair '+ '(1 3))))
#+end_src
#+RESULTS: built-in-pair
: evaluates to =>
: (1 . yes)
: (+)
: (+ 1 3)
: 4
- =first= :: (=regular function [C++]=) Takes a list as argument and returns the contents of its
=first= field.
{{{slime_header}}}
#+name: built-in-first
#+begin_src slime
(printf (first (pair 1 3)))
(printf (first (list 2 3)))
(printf (first '("hello" "world")))
#+end_src
#+RESULTS: built-in-first
: evaluates to =>
: 1
: 2
: hello
- =rest= :: (=regular function [C++]=) Takes a list as argument and returns the contents of its
=rest= field.
{{{slime_header}}}
#+name: built-in-rest
#+begin_src slime
(printf (rest (pair 1 3)))
(printf (rest (list 2 3)))
(printf (rest '("hello" "world")))
#+end_src
- =list= :: (=regular function [C++]=) Takes any number of arguments, evaluates each and returns a
list containing the results.
{{{slime_header}}}
#+name: built-in-list
#+begin_src slime
(printf (list))
(printf (list 1 2 3))
(printf (list (pair 1 2)
'(3 4)
(list 5 6)))
#+end_src
#+RESULTS: built-in-list
: evaluates to =>
: ()
: (1 2 3)
: ((1 . 2) (3 4) (5 6))
- =length= :: (=regular function [Slime]=) Takes a list as argument and returns the number of
elements in that list.
{{{slime_header}}}
#+name: built-in-length
#+begin_src slime
(printf (length ()))
(printf (length '(1 2 3)))
(printf (length '(+ 1 4 (+ 2 3))))
#+end_src
#+RESULTS: built-in-length
: evaluates to =>
: 0
: 3
: 4
- =end= :: (=regular function [Slime]=) Takes a list as argument. Returns the last pair in the
list.
{{{slime_header}}}
#+name: built-in-end
#+begin_src slime
(printf (end ()))
(printf (end '(1 2 3)))
(printf (end '(+ 1 4 (+ 2 3))))
#+end_src
#+RESULTS: built-in-end
: evaluates to =>
: ()
: (3)
: ((+ 2 3))
- =last= :: (=regular function [Slime]=) Takes a list as argument. Returns the last element in the
list.
{{{slime_header}}}
#+name: built-in-last
#+begin_src slime
(printf (last ()))
(printf (last '(1 2 3)))
(printf (last '(+ 1 4 (+ 2 3))))
#+end_src
#+RESULTS: built-in-last
: evaluates to =>
: ()
: 3
: (+ 2 3)
- =extend= :: (=regular function [Slime]=) Takes a list and any
- =append= :: (=regular function [Slime]=)
- =append= :: (=regular function [Slime]=)
- =length= :: (=regular function [Slime]=)
- =range= :: (=regular function [Slime]=)
- =range= :: (=regular function [Slime]=)
- =range-while= :: (=regular function [Slime]=)
- =range-while= :: (=regular function [Slime]=)
@@ -530,8 +916,8 @@ embedded scripting language.
** no category
** no category
- =eval= :: (=regular function [C++]=)
- =eval= :: (=regular function [C++]=)
- =apply= :: (=regular function [C++]=)
- =apply= :: (=regular function [C++]=)
- =lambda= :: (=regular function [C++]=)
- =special-lambda= :: (=regular function [C++]=)
- =lambda= :: (=special form [C++]=) See the section about =Lambdas= in [[Lambdas]].
- =special-lambda= :: (=special form [C++]=) See the section about =Lambdas= in [[Lambdas]].
- =copy= :: (=regular function [C++]=)
- =copy= :: (=regular function [C++]=)
- =import= :: (=regular function [C++]=)
- =import= :: (=regular function [C++]=)
@@ -542,7 +928,7 @@ embedded scripting language.
- =quasiquote= :: (=regular function [C++]=)
- =quasiquote= :: (=regular function [C++]=)
- =unquote= :: (=regular function [C++]=)
- =unquote= :: (=regular function [C++]=)
- =mutate= :: (=regular function [C++]=)
- =mutate= :: (=regular function [C++]=)
- =define= :: (=regular function [C++]=)
- =define= :: (=special form [C++]=) See the section about =define= in [[Define]].
- =assert= :: (=regular function [C++]=)
- =assert= :: (=regular function [C++]=)
* testbox :noexport:
* testbox :noexport:
#+BEGIN_SRC ditaa :file diagrams/test.eps :cmdline --no-separation --no-shadows
#+BEGIN_SRC ditaa :file diagrams/test.eps :cmdline --no-separation --no-shadows