COP4020 Programming Languages Functional Programming Prof. Xin Yuan Topics Functional programming with Scheme Learn by examples 5/29/2016 COP4020 Spring 2014 2 Defining Global Names A global name is defined with the “define” special form (define name value) Usually the values are functions (lambda abstractions) Examples: 5/29/2016 (define my-name ''foo'') (define determiners '(''a'' ''an'' ''the'')) (define sqr (lambda (x) (* x x))) (define twice (lambda (f a) (f (f a)))) (twice sqr 3) ((lambda (f a) (f (f a))) (lambda (x) (* x x)) 3) … 81 COP4020 Spring 2014 3 Defining Global Names A global name defined with a lambda express in essence creates a named function. This allows recursion – one of the most important/useful concept in scheme. (define fab (lambda (x) (if (= x 0) 0 (if (=x 1) 1 (+ (fab (- x 1)) (fab (- x 2))))))) Define a function that computes the sum of all elements in a list. Define a function that put an element at the end of a list 5/29/2016 COP4020 Spring 2014 4 Load program from file Load function (load filename) load the program from a file (load “myscheme”): load the program in “myscheme.scm” After load, one has access to all global names in the program. 5/29/2016 COP4020 Spring 2014 5 I/O (display x) prints value of x and returns an unspecified value (newline) advances to a new line (read) returns a value from standard input (display "Hello World!") Displays: "Hello World!" (display (+ 2 3)) Displays: 5 (if (member (read) '(6 3 5 9)) "You guessed it!" "No luck") Enter: 5 Displays: You guessed it! (read-line) returns the string in a line (without the “\n”). 5/29/2016 COP4020 Spring 2014 6 I/O with files Open file for reading: (open-input-file filename) Open file for writing: (open-output-file filename) Read one character from a file: (read-char file) Read a scheme object from file: (read file) Check the current character in a file without consuming the character: (peek-char file) Check end-of-file: (eof-object? (peek-char file)) Write one character to a file: (write-char char file) Write a scheme object to file: (write object file) Close file: (close-port file) 5/29/2016 COP4020 Spring 2014 7 Blocks (begin x1 x2 … xn) sequences a series of expressions xi, evaluates them, and returns the value of the last one xn Examples: 5/29/2016 (begin (display "Hello World!") (newline) ) COP4020 Spring 2014 8 Example 1: define factorial function: fact(n) = 1*2*3*…*n 5/29/2016 COP4020 Spring 2014 9 Example 1: define factorial function: fact(n) = 1*2*3*…*n Recursive factorial: (define fact (lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))) ) ) (fact 2) (if (zero? 2) 1 (* 2 (fact (- 2 1)))) (* 2 (fact 1)) (* 2 (if (zero? 1) 1 (* 1 (fact (- 1 1))))) (* 2 (* 1 (fact 0))) (* 2 (* 1 (if (zero? 0) 1 (* 0 (fact (- 0 1)))) (* 2 (* 1 1)) 2 5/29/2016 COP4020 Spring 2014 10 Example 2 Sum the elements of a list (define sum (lambda (lst) (if (null? lst) 0 (+ (car lst) (sum (cdr lst))) ) ) ) (sum '(1 2 3)) (+ 1 (sum (2 3)) (+ 1 (+ 2 (sum (3)))) (+ 1 (+ 2 (+ 3 (sum ())))) (+ 1 (+ 2 (+ 3 0))) 5/29/2016 COP4020 Spring 2014 11 Example 3 Generate a list of n copies of x (define fill (lambda (n x) ??? ) ) 5/29/2016 COP4020 Spring 2014 12 Example 3 Generate a list of n copies of x (define fill (lambda (n x) (if (= n 0) () (cons x (fill (- n 1) x))) ) ) (fill 2 'a) (cons a (fill 1 a)) (cons a (cons a (fill 0 a))) (cons a (cons a ())) (a a) 5/29/2016 COP4020 Spring 2014 13 Example 4 Replace x with y in list xs (define subst (lambda (x y xs) ??? ) ) 5/29/2016 COP4020 Spring 2014 14 Example 4 Replace x with y in list xs (define subst (lambda (x y xs) (cond ((null? xs) ()) ((eq? (car xs) x) (cons y (subst x y (cdr xs)))) (else (cons (car xs) (subst x y (cdr xs)))) ) ) ) 5/29/2016 COP4020 Spring 2014 15