Scheme, a derivative of LISP (LISt Processing language, or Lots of Irritating Superfluous Parentheses), seems like it would be a syntax strict, extremely picky language with lots of strange functions, commands, etc. *This is a myth. *The R5RS standard specifies what a complient Scheme interpreter/compiler should do (if you do not yet have an interpreter/compiler, check out either DrScheme or MIT Scheme.)
For these lessons, you will only need an interpreter. *I do not think that either of the ones I mentioned above are better than the other, so make a choice. *If you find another interpretter that is R5RS complient, then by all means, try it out. *If you have problems with DrScheme, I will be able help more with that interpreter than the others as that is the one I am most familiar with. *I will try to help with others, but I will probably not be as confident. *Please post your code so that I and others can first make sure that it is not in the code itself.
Scheme is case insensitive, so, if you insist, you can write all your code with the caps lock on and not care, but all my code will be in lower case. *The only exception to that rule are strings, but that will be covered later.
All programming tutorials have the learner first see a program written in that language that says hello world. *Having you learn how to output "Hello World!" seems less important than having you understand lists since the understanding of lists is absolutely essential to learning Scheme (do not worry though, we will get into strings eventually, which are case sensitive.) *Thus, for our first program, I will show a few pieces of code that will create a list containing two symbols: hello and world. *This will be my compromise for not having a "Hello World!" program.
Both of these s-expressions (a list of symbols) should output the same thing: (hello world). *What are these two s-expressions doing? *The first, with the list function (notice how the function comes first)*, takes a variable number of arguments (notice how the arguments all come after the function)*, and puts them all in a list. *The second, with the cons function, takes two arguments: the first is any datum you would like it to be (it can even be another list) and the second has to be a list (well, technically, it can be anything, but for now, it has to be a list.)
(list 'hello 'world)
(cons 'hello '(world))
Notice how there is a single quote before hello, world, and (world). *These are needed since neither hello nor world are previously defined. *If you eliminate the single quotes, your interpreter will complain. *Also, note that the single quote tells Scheme that the symbol immediately after the quote or symbols within the list that the quote prepends will not be interpreted as Scheme functions. *So take the following for example:
Now you have these lists, but you need to get at certain parts of the list. *To start out, an examination of car and cdr (pronounced 'cudder'), leftovers from LISP. *Both car and cdr take one argument, a list. *Car will take the list and return you the first element in that list. *Cdr will take the list and return you the list without the car.
(list 'list 'hello 'world)
(cons 'cons '(hello world))
The first s-expression will return the symbol h. *The second will return a list, (e l l o).
(car '(h e l l o))
(cdr '(h e l l o))
If you do not understand prefix notation yet, then keep reading. *If you do, then you can look at the problems I have provided.
In math, the norm is to use infix notation. *The add function in 4 + 5 is fixed inside our mathematical expression. *With prefix notation, that same expression would look like (+ 4 5). *To tie it into some of the rest of this lesson, the function is always the car of a list, and its arguments are the cdr of the list.
1. What does the following return?
(car (cdr '(h e l l o)))
2. How would you return the second l in the list from the previous question?
3. Take the following list: (a b (c d) e f). *Figure out a way to return c, d, and finally f. *Note: I am not asking you to return a list with c, d, and f. *Just use three different s-expressions to return each of these symbols.
4. The list function is nothing more than syntactic sugar (i.e. it is not needed as other constructs can do the same thing, but it may not be as pretty.) *How could you construct a list containing the symbols foo, bar, and baz without using the list function (using what we have learnt thusfar)?
* Scheme uses prefix notation. *This means that the function you want to use must come first before its arguments. *If you want to perform another function on some other data, then you need another list. *So, if you wanted to cons 'a on to (list 'b 'c), the s-expression would look like: (cons 'a (list 'b 'c)).