Scheme is a Dialect of Lisp
What are people saying about Lisp? • "The greatest single programming language ever designed." -Alan Kay, co-inventor of Smalltalk and OOP • "The only computer language that is beautiful." -Neal Stephenson, John's favorite sci-fi author • "God's programming language." 61A Lecture 25 -Brian Harvey, Berkeley CS instructor extraordinaire
Friday, October 26
http://imgs.xkcd.com/comics/lisp_cycles.png
2
Scheme Fundamentals Special Forms
Scheme programs consist of expressions, which can be: A combination that is not a call expression is a special form:
• If expression: (if
Numbers are self-evaluating; symbols are bound to values. • New procedures: (define (
Call expressions have an operator and 0 or more operands. > (define pi 3.14) The name “pi” is bound to > (quotient 10 2) “quotient” names Scheme’s > (* pi 2) 3.14 in the global frame 5 built-in integer division 6.28 > (quotient (+ 8 7) 5) procedure (i.e., function) 3 > (define (abs x) A procedure is created and > (+ (* 3 (if (< x 0) bound to the name “abs” (+ (* 2 4) Combinations can span (- x) (+ 3 5))) multiple lines x)) (+ (- 10 7) (spacing doesn’t matter) > (abs -3) 3 6)) Demo Demo
3 4
Lambda Expressions Pairs and Lists In the late 1950s, computer scientists used confusing names. Lambda expressions evaluate to anonymous functions. • cons: Two-argument procedure that creates a pair • car: Procedure that returns the first element of a pair (lambda (
5 6 Symbolic Programming Scheme Lists and Quotation
Symbols normally refer to values; how do we refer to symbols? Dots can be used in a quoted list to specify the second element of the final pair. > (define a 1) > (define b 2) No sign of “a” and “b” in > (cdr (cdr '(1 2 . 3))) > (list a b) the resulting value 3 (1 2) However, dots appear in the output only of ill-formed lists. Quotation is used to refer to symbols directly in Lisp. > '(1 2 . 3) > (list 'a 'b) 1 2 3 (1 2 . 3) (a b) Symbols are now values > '(1 2 . (3 4)) > (list 'a b) 1 2 3 4 nil (1 2 3 4) (a 2) > '(1 2 3 . nil) 1 2 3 nil Quotation can also be applied to combinations to form lists. (1 2 3)
> (car '(a b c)) What is the printed result of evaluating this expression? a > (cdr '(a b c)) > (cdr '((1 2) . (3 4 . (5)))) (b c) (3 4 5)
7 8
Coercing a Sorted List to a Binary Search Tree The Let Special Form
1 2 3 4 5 6 7 1 2 3 4 5 6 7 (define (entry tree) ...) (define (left-branch tree) ...) 4 (define (right-branch tree) ...) (define (make-tree entry left right) ...) (define (list->tree elements) 2 6 (car (partial-tree elements (length elements))))
(define (partial-tree elts n) (if (= n 0) 1 3 5 7 (cons nil elts) (let ((left-size (quotient (- n 1) 2))) (let ((left-result (partial-tree elts left-size))) (let ((left-tree (car left-result)) Divide length n into 3 parts: [ (n-1)/2 , 1 , (n-1)/2 ] (non-left-elts (cdr left-result)) (right-size (- n (+ left-size 1)))) Recursively coerce the left part (let ((this-entry (car non-left-elts)) (right-result (partial-tree (cdr non-left-elts) right-size))) The next element is the entry (let ((right-tree (car right-result)) (remaining-elts (cdr right-result))) Recursively coerce the right part (cons (make-tree this-entry left-tree right-tree) remaining-elts))))))))
9 10
The Begin Special Form
(begin
Demo
11