# Introduction **When was the original Lisp designed?** The original Lisp was designed *in 1958.* > **Who designed the original Lisp?** > The original Lisp was designed by *John McCarthy.* **What are the two main dialects of the original Lisp?** The two main dialects of the original Lisp are: 1. Scheme. 2. Common Lisp. **What is the only other programming language which is older than the original Lisp and by how long?** The only other programming language which is older than the original Lisp is *Fortran by one year.* > **Has Fortran changed drastically?** > *Yes*, Fortran has changed drastically. > > **Has Lisp changed drastically?** > *No*, Lisp hasn't changed drastically. **What paradigm is the pure subset of Lisp?** The pure subset of Lisp is *functional.* > **What is the pure subset of Lisp based on?** > The pure subset of Lisp is based on *the lambda calculus of Alonzo Church.* > [!quote] Quote by Jorge Santayana > Those who cannot remember the past are condemned to repeat it. **Those who don't know Lisp are doomed to ... ..., ...** Those who don't know Lisp are doomed to *reinvent it*, *poorly.* **What are the features of Scheme?** The features of Scheme include: * Extremely simple syntax for programs and data. * Strongly and dynamically typed. * Statically scoped. * Higher-order - Functions are first-class objects which can be constructed and evaluated during execution. * Embeddable - Guile (Scheme dialect) is used in programs such as GnuCash, LilyPond, GNU Guix, GNU Debugger, GNU TeXmacs, GNU Make, and Google's Schism. > [!example] Example of a GNU makefile which uses Scheme code through the Guile interpreter > ```makefile > # PATH=/bin:/usr/bin LD_LIBRARY_PATH= make > > all: > echo $(addsuffix .class,$(basename $(wildcard *.java))) > $(guile (let ((msg (list "Hello " "world\n"))) \ > (display (car msg)) \ > (display (cadr msg)))) > ``` # Program structure **What is a Scheme program made up of?** A Scheme program is made up of *a set of function definitions followed by a sequence of function calls.* **What is the function definition syntax in Java?** The function definition syntax in Java is: $ t_f\ f(t_1\ p_1,t_2\ p_2,\dots,t_n\ p_n)\ \{\ body\ \} $ **What is the function definition syntax in Scheme?** The function definition syntax in Scheme is: $ (\textrm{define}\ (f\ p_1\ p_2\ \cdots\ p_n)\ body) $ **What is the function call syntax in Java?** The function call syntax in Java is: $ f(p_1,p_2,\dots,p_n) $ **What is the function call syntax in Scheme?** The function call syntax in Scheme is: $ (f\ p_1\ p_2\ \cdots\ p_n) $ > **What does an open parenthesis always mean in Scheme?** > In Scheme, an open parenthesis always means *you're calling a function.* > [!example] Example of a Scheme program which defines a recursive function for summing a sequence of numbers, calls it, and displays the results > ```scheme > ;; Scheme sum program > > (define (sum seq) > (if (null? seq) > 0 > (+ (car seq) (sum (cdr seq))))) > > (display (sum '(5 6 1 8 3 7))) > (display "\n") > ``` **What does the abbreviation `'x` mean?** The abbreviation `'x` means *`(quote x)`.* > **What does the abbreviation `'(...)` mean?** > The abbreviation `'(...)` means `(quote (...))`. **What does the built-in function `quote` return?** The built-in function `quote` returns *its one parameter, unevaluated.* **What is a function definition in Scheme?** In Scheme, a function definition is *a call to a function called `define` which defines a function.* > [!example] Example of a Scheme program which defines a tail recursive function for summing a sequence of numbers, calls it, and displays the results > ```scheme > ;; Scheme sum program > > (define (sum seq) > (define (sum seq res) > (if (null? seq) > res > (sum (cdr seq) (+ res (car seq))))) > (sum seq 0)) > > (display (sum '(5 6 1 8 3 7))) > (display "\n") > ``` **What does the built-in function `car` do?** The built-in function `car` *returns the first value in a sequence.* **What does the built-in function `cdr` do?** The built-in function `cdr` *returns all of the values in a sequence except the first.* **What can a translator or compiler do if it recognizes a tail recursive function?** If a translator or compiler recognizes a tail recursive function, it can *optimize the translated or compiled code by utilizing jump instructions.* **Does the `define` function cause a side effect?** *Yes*, the `define` function causes a side effect. **Can the `define` function redefine a symbol?** *Yes*, the `define` function can redefine a symbol. **Is the `define` function an imperative feature and when?** *Yes*, the `define` function is an imperative feature *if misused.* **What makes a function definition and a variable definition similar in Scheme?** In Scheme, a function definition and a variable definition are similar in that *both simply bind a value to a symbol (its name).* > **What makes a function definition and a variable definition dissimilar in Scheme?** > In Scheme, a function definition and a variable definition are dissimilar in that *the type of the value is different.* **How do you denote a callable value in Scheme?** In Scheme, you denote a callable value with *`(lambda ...)`.* > [!example] Example of defining a named function in Scheme with the `define` function, calling it, and displaying it > ```scheme > guile> (define (f x) (+ 1 x)) > guile> (f 123) > $1 = 124 > guile> f > $2 = #<procedure f (x)> > ``` > [!example] Example of defining a variable in Scheme to hold the returned value of the `+` function > ```scheme > guile> (define f (+ 1 123)) > guile> f > $1 = 124 > ``` > [!example] Example of defining a variable in Scheme to hold a function created with the `lambda` function > ```scheme > guile> (define f (lambda (x) (+ 1 x))) > guile> (f 123) > $1 = 124 > guile> f > $2 = #<procedure f (x)> > ``` > [!example] Example of calling a function created by the `lambda` function without giving it a name > ```scheme > guile> ((lambda (x) (+ 1 x)) 123) > $1 = 124 > ``` # Syntax and Semantics ... **What does a Scheme program consist of?** A Scheme program consists of *symbolic expressions called S-expressions.* **What does a literal evaluate itself to?** A literal evaluates itself to *itself.* **What does a symbol evaluate to?** A symbol evaluates to *its defined value*. **What does a paranthesized sequence of S-expressions evaluate to?** A paranthesized sequence of S-expressions evaluates to *the return value of first S-expression in the sequence being called as a function and the remaining S-expressions being passed to that function as parameters.* **How are some parameters to some functions evaluated?** Some parameters to some functions are evaluated *after the function call.* # Translation **How is a Scheme program interpreted?** A Scheme program is interpreted *by the read-eval-write loop which calls three built-in functions of the same names.* **What does the built-in `read` function do?** The built-in `read` function *reads a complete S-expression from `stdin` and returns it.* > **What does the `read` function do if it is paranthesized?** > If the `read` function is paranthesized, it *constructs a list to contain it.* **What does the built-in `eval` function do?** The built-in `eval` function *evaluates and returns the result of `read`.* **What does the built-in `write` function do?** The built-in `write` function *writes the result of `eval` to `stdout`.* # List representation **What is the list created by the `read` function composed of?** The list created by the `read` function is composed of *pairs and atoms.* **What does the `read` function return if it constructs a list?** If the `read` function constructs a list, it returns *a reference to the list.* **How is a pair created?** A pair is created *by the `cons` function.* **What two functions return the different parts of a pair?** The functions that return the different parts of a pair are: 1. `car`. 2. `cdr`. **Do you need to explicitly end a list with an empty list, null, or nil value?** *No*, you don't need to explicitly end a list with an empty list, null, or nil value. **What is `(a b c)` an abbreviation for?** `(a b c)` is an abbreviation for *`(a b c . '())`.* > [!example] Examples of S-expressions you could pass to `read` and `eval` to produce the list `(a b c)` > * `'(a b c)`. > * `(quote (a b c))`. > * `(list 'a 'b 'c)`. > * `(cons 'a (cons 'b (cons 'c '())))`.