# Structure andInterpretationof ComputerPrograms

Fall 2021
-->

## Mon Sep 6 - scheme 101

So ... how are things going?

Some of you have turned in work ... others have not.

Let's discuss the scheme language, some convenience features that I've added ... and maybe skip ahead to some power tools that some of you may already be looking for.

## some "jupyter scheme" language notes

• Describe how to set up scheme on your computer (mac or windows subsystem for linux or linux)

* First: "brew install guile@2" (mac) or "apt install guile-2.2" (linux)
* Second: install my customizations ... see the resources page, and I'll discuss


• guile (scheme on jupyter.bennington) "commands" and bindings

If you're using the terminal scheme on jupyter.bennington (as I am most of the time), you may have noticed that it suggest typing ,help. These things you can type at the interactive prompts, starting with a comma, are not part of the scheme language but meta-functions that let give you access to some other interactive-oriented functionality.

One of these is ,apropos which will show you things that match a regular expression that you type. And since the regular expression .* matches everything, you can use that to get a list of all the functions, macros, variables, etc that are defined in the current environment. There are a lot of them.

I've put a link that that output on the resources page, jupyter_scheme_default_symbols.txt. You can ,describe most of them to learn more.

begin

One useful built-in is begin , which lets you execute a series of functions: do this, do that, do the other thing.

(begin
(display "Hello!")
(newline)
(display "Goodbye!")
(newline))


The last value is returned, so in (begin a b c), the return value is c.

(Aside: expressions in scheme have "return values" ...)

display, newline, and printf (not in textbook; too useful not to have)

(define a 4)
(printf "The value of ~a is ~a.\n" "a" a)


• "evaluation"

... a crucial concept. Discuss, and compare with some examples from other languages.

(What do you get when you eval a number? a string? a symbol? Hmmm. symbols. Let's do that a bit later.)

"applicative order" : what happens to (f (+ 1 2) (* 3 4)) ?

"normal order": what happens to (f (+ 1 2) (* 3 4)) ?

How can you tell?

;; evaluation, boolean operators, and "shortcuts"
;; ... or why "and" and "or" are "special forms"

(define (pr x y)
;; print-return : print something, return y
(begin
(printf "(pr ~a ~a)\n" x y)
y))

;; what gets evaluated in this , and why?
(printf "-- and --\n")
(and (pr "one" #t) (pr "two" #f) (pr "three" #t))

(printf "-- or --\n")
(or (pr "one" #t) (pr "two" #f) (pr "three" #t))

;; Can you try this in python?


Do languages like (python, C, javascript, ...) have these complications? Let's discuss some examples.

• "special form"

... anything in scheme that has a "special" way of evaluating its arguments : if , cond, define, or, and, ...

... can we define our own "special forms"? Yes! They're called "macros"; in scheme the most common way to create them is with define-syntax.

(This is outside the textbook's presentation, so not something you should need for the homework ... but can be very, very handy and powerful.)

Google "scheme define-syntax" to find explanations, such as Shido's .

Or look in my init-guile.scm for examples.

(load "filename.scm")

You can import scheme code into the interactive prompt with the load builtin .

;; -- file define-a.scm --
(define a "This is A!")

\$ scheme
> a
"This is A!"


• types, predicates, symbols, and quote

(This is jumping ahead a bit, but context that I think is helpful.)

The basic types of data in scheme are described in Dorai's simple data types in "Teach Yourself Scheme in Fixnum Days". We'll do lists - in great detail - later, but for now the atomic data types are worth taking note of :

• numbers : integers (42), rationals (3/4), reals (3.14159), complex (3-2i) .
• predicates (number? x) , (integer? x), etc
• strings : "Hello...\n" where \n is (as usual) the newline character.

Evaluating a number gives the same number; evaluating a string gives that string. (Scheme also has both mutable and immutable strings, but we don't need that complication yet.)

Scheme also has "symbols", which can point to other things ... that "binding" is what define does. Evaluating a symbol follows the pointer to what its bound to.

You can get the symbol itself, without evaluating it, with the special quote function :

> (quote a)
a


The quote is used often enough that it has a short form abbreviation (exactly the same but spelled differently)

> a
a


This lets you do fun tricks like

> (define a (quote b))
> (define b (quote a))
> a  ;; follow the pointer ...
b
> b
a
`

... which would be a tricky to express in a language like python. Though it can be translated pretty quickly to C.

There are also conversion functions like symbol->string and string->symbol.