CS 403 Programming Languages Lecture 6 Fall 2003













- Slides: 13

CS 403: Programming Languages Lecture 6 Fall 2003 Department of Computer Science University of Alabama Joel Jones Lecture 6 © 2003 Joel Jones

Outline Questions n Introduction to Scheme n Reading for next time n Lecture 6 © 2003 Joel Jones 2

Questions How did the exercises on Squeak go? n Any questions on MP 1? n Lecture 6 © 2003 Joel Jones 3

What is Functional Programming? Computation using functions n Functions are roughly equivalent to those in mathematics n Functions are first class objects n Define anywhere n Pass as arguments—downward fun(ction) arg(ument)s n Return as results—upward funargs n Lecture 6 © 2003 Joel Jones 4

Where have we seen this before? n Smalltalk blocks are typically used as downward funargs n In bool. Val if. True: [ show a ] the block with show a is an anonymous function passed as an argument to the if. True message to bool. Val Lecture 6 © 2003 Joel Jones 5

S-expressions s-expression – a symbol, a number, or a list (S 1, …, Sn) of zero or more Sexpressions n nil list – list of zero elements () n Function application has operator as first element of list and operators following n n Example: (+ 1 2 3) returns 6 Lecture 6 © 2003 Joel Jones 6

Operations on S-Expressions: n n n n car: first element of list or nil cdr: everything in list except first element cons: if S = (S 1, …, Sn), then (cons S’ S) is (S’ S 1, …, Sn) =: returns T if equal, nil otherwise, for non-lists and empty lists number? , symbol? , list? , null? : T if argument of type, nil otherwise +, -, *, / <, >: T if both numbers and condition holds, nil otherwise Lecture 6 © 2003 Joel Jones 7

Syntax value -> integer | quoted-const n value-op -> + | - | * | / | = | < | > | cons | car | cdr |number? | symbol? | list? | null? | print n quoted-const -> ‘S-expression n S-expression -> integer | symbol | ( Sexpression* ) n symbol -> name n Lecture 6 © 2003 Joel Jones 8

Let’s Play at the Board Cons cells n (cons ‘a ()) n (cons ‘a ‘(b)) n (cons ‘(a) ‘(b)) n Lecture 6 © 2003 Joel Jones 9

Example of a list using function n (define length (lambda (l) (if (null? l) 0 (+1 (length (cdr l)))))) Lecture 6 © 2003 Joel Jones 10

Let’s play at the board again (Write the following functions) n n n (list 1 x) - list of one element, x (list 2 x y) - list of two elements, x and y (list 3 x y z) - list of three elements, x, y, and z (atom? x) - T if null, number, or symbol, nil otherwise (equal l 1 l 2) - T if two equal atoms or equal lists, nil otherwise. Two lists are equal if same length and corresponding elements are equal. Lecture 6 © 2003 Joel Jones 11

Larger Scheme Example n Calculate prime numbers less than n using Sieve of Eratosthenes. (code will be on web site) Lecture 6 © 2003 Joel Jones 12

Reading & Questions for Next Class n Online version of the book “Structure and Interpretation of Computer Programs” at: http: //mitpress. mit. edu/sicp/ through and including 1. 1. 8 n n n Note: (define f (lambda (n (+ n 1))) is defined there as (define (f n) (+ n 1)) What data structures can be represented using Scheme-like lists? What data structures can’t be represented using Scheme-like lists? Lecture 6 © 2003 Joel Jones 13