Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 403: Programming Languages Lecture 6 Fall 2003 Department of Computer Science University of Alabama Joel Jones."— Presentation transcript:

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

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

3 Lecture 6©2003 Joel Jones3 Questions How did the exercises on Squeak go? Any questions on MP1?

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

5 Lecture 6©2003 Joel Jones5 Where have we seen this before? Smalltalk blocks are typically used as downward funargs In boolVal ifTrue: [ show a ] the block with show a is an anonymous function passed as an argument to the ifTrue message to boolVal

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

7 Lecture 6©2003 Joel Jones7 Operations on S-Expressions: car: first element of list or nil cdr: everything in list except first element cons:if S = (S 1,…,S n ), then (cons S’ S) is (S’ S 1,…,S n ) =: 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

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

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

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

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

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

13 Lecture 6©2003 Joel Jones13 Reading & Questions for Next Class Online version of the book “Structure and Interpretation of Computer Programs” at:http://mitpress.mit.edu/sicp/ through and including 1.1.8http://mitpress.mit.edu/sicp/ 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?


Download ppt "CS 403: Programming Languages Lecture 6 Fall 2003 Department of Computer Science University of Alabama Joel Jones."

Similar presentations


Ads by Google