Comparative Programming Languages Functional programming with Lisp/Scheme
CS 363 Spring 2005 GMU2 Fundamentals of Functional Programming Languages The objective of the design of a functional programming language (FPL) is to mimic mathematical functions to the greatest extent possible The basic process of computation is fundamentally different in a FPL than in an imperative language –In an imperative language, operations are done and the results are stored in variables for later use –Management of variables is a constant concern and source of complexity for imperative programming In an FPL, variables are not necessary, as is the case in mathematics
CS 363 Spring 2005 GMU3 Lisp Lisp – based on lambda calculus (Church) –Uniform representation of programs and data using single general data structure (list) –Interpreter based (written in Lisp) –Automatic memory management –Evolved over the years –Dialects: COMMON LISP, Scheme
CS 363 Spring 2005 GMU4 Scheme Scheme is a functional programming language and a dialect of Lisp. It was developed by Guy L. Steele and Gerald Jay Sussman at MIT Use Programming Languages ->Dr. Scheme here in the Comp Sys lab
CS 363 Spring 2005 GMU5 Common Lisp Use: clisp for Common Lisp here in the systems lab
CS 363 Spring 2005 GMU6 Scheme (dr scheme, guile) (define (gcd u v) (if ( = v 0) u (gcd v (remainder u v)) ) (define (reverse l) (if (null? l) l (append (reverse (cdr l))(list (car l))) )
CS 363 Spring 2005 GMU7 Scheme (dr scheme, guile) Using guile (gnu scheme): (load "slides.scm") (gcd ) --> 4 (reverse '( )) --> ( )
8 Common Lisp (clisp) (defun mygcd (u v) (if (= v 0) u (mygcd v (rem u v)) ) (defun myreverse (l) (if (null l) l (append (myreverse (cdr l))(list (car l))) ) ;; (load "slides.lsp"), (mygcd ) --> 4 ;; (myreverse '( )) --> ( )
9 Scheme (define (gcd u v) (if ( = v 0) u (gcd v (remainder u v)) ) Once defined in the interpreter: (gcd 25 10) 5
10 Scheme/Lisp expression → atom | list atom → number | string | identifier | character | boolean list → ‘(‘ expression-sequence ‘)’ expression-sequence → expression | expression-sequence expression
11 Scheme Expression vs. C In Scheme: (+ 3 (* 4 5 )) (and (= a b)(not (= a 0))) (gcd 10 35) In C: * 5 (a = = b) && (a != 0) gcd(10,35)
12 Evaluation Rules for Scheme Expressions 1.Constant atoms (numbers, strings) evaluate to themselves 2.Identifiers are looked up in the current environment and replaced by the value found there (using dynamically maintained symbol table) 3.A list is evaluated by recursively evaluating each element in the list as an expression; the first expression must evaluate to a function. The function is applied to the evaluated values of the rest of the list.
13 Scheme Evaluation To evaluate (* (+ 2 3)(+ 4 5)) 1.* is the function – must evaluate the two expressions (+ 2 3) and (+ 4 5) 2.To evaluate (+ 2 3) 1.+ is the function – must evaluation the two expressions 2 and evaluates to the integer evaluates to the integer = 5 3.To evaluate (+ 4 5) follow similar steps 4.* 5 9 = 45 *
14 Scheme Conditionals If statement: (if ( = v 0) u (gcd v (remainder u v)) ) (if (= a 0) 0 (/ 1 a)) Cond statement: (cond (( = a 0) 0) ((= a 1) 1) (else (/ 1 a)) )
15 Example of COND (Scheme) (DEFINE (compare x y) (COND ((> x y) (DISPLAY “x is greater than y”)) ((< x y) (DISPLAY “y is greater than x”)) (ELSE (DISPLAY “x and y are equal”)) )
16 Example of COND (Lisp) (defun compare (x y) (COND ((> x y)(format t “x is greater than y”)) ((< x y)(format t “y is greater than x”)) (t (format t “x and y are equal”)) )
17 Predicate Functions (Scheme) EQ? takes two symbolic parameters; it returns #T if both parameters are atoms and the two are the same e.g., (EQ? 'A 'A) yields #T (EQ? 'A '(A B)) yields () –Note that if EQ? is called with list parameters, the result is not reliable –EQ? does not work for numeric atoms (use = )
18 Predicate Functions (Lisp) EQ takes two symbolic parameters; it returns #T if both parameters are atoms and the two are the same e.g., (eq 'A 'A) yields #T (eq 'A '(A B)) yields nil
19 Predicate Functions (Scheme) LIST? takes one parameter; it returns #T if the parameter is a list; otherwise () 3. NULL? takes one parameter; it returns #T if the parameter is the empty list; otherwise () Note that NULL? returns #T if the parameter is () 4. Numeric Predicate Functions =, <>, >, =, <=, EVEN?, ODD?, ZERO?, NEGATIVE?
20 Predicate Functions (Lisp) LISTP takes one parameter; it returns #T if the parameter is a list; otherwise nil 3. NULL takes one parameter; it returns #T if the parameter is the empty list; otherwise nil Note that NULL returns #T if the parameter is () 4. Numeric Predicate Functions =, <>, >, =, <=, EVENP, ODDP, ZEROP