cop4020 programming languages functional programming prof. xin yuan

15
COP4020 Programming Languages Functional Programming Prof. Xin Yuan

Upload: ross-tyler

Post on 17-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COP4020 Programming Languages Functional Programming Prof. Xin Yuan

COP4020Programming Languages

Functional Programming

Prof. Xin Yuan

Page 2: COP4020 Programming Languages Functional Programming Prof. Xin Yuan

COP4020 Spring 2014 204/21/23

Topics

Functional programming with Scheme Learn by examples

Page 3: COP4020 Programming Languages Functional Programming Prof. Xin Yuan

COP4020 Spring 2014 304/21/23

Defining Global Names

A global name is defined with the “define” special form

(define name value)

Usually the values are functions (lambda abstractions) Examples:

(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

Page 4: COP4020 Programming Languages Functional Programming Prof. Xin Yuan

COP4020 Spring 2014 404/21/23

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

Page 5: COP4020 Programming Languages Functional Programming Prof. Xin Yuan

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.

COP4020 Spring 2014 504/21/23

Page 6: COP4020 Programming Languages Functional Programming Prof. Xin Yuan

COP4020 Spring 2014 604/21/23

I/O

(display x) prints value of x and returns an unspecified value (display "Hello World!")

Displays: "Hello World!" (display (+ 2 3))

Displays: 5

(newline) advances to a new line (read) returns a value from standard input

(if (member (read) '(6 3 5 9)) "You guessed it!" "No luck")Enter: 5Displays: You guessed it!

(read-line) returns the string in a line (without the “\n”).

Page 7: COP4020 Programming Languages Functional Programming Prof. Xin Yuan

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)

COP4020 Spring 2014 704/21/23

Page 8: COP4020 Programming Languages Functional Programming Prof. Xin Yuan

COP4020 Spring 2014 804/21/23

Blocks

(begin x1 x2 … xn) sequences a series of expressions xi, evaluates them, and returns the value of the last one xn

Examples: (begin

  (display "Hello World!")   (newline) )

Page 9: COP4020 Programming Languages Functional Programming Prof. Xin Yuan

COP4020 Spring 2014 904/21/23

Example 1: define factorial function: fact(n) = 1*2*3*…*n

Page 10: COP4020 Programming Languages Functional Programming Prof. Xin Yuan

COP4020 Spring 2014 1004/21/23

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

Page 11: COP4020 Programming Languages Functional Programming Prof. Xin Yuan

COP4020 Spring 2014 1104/21/23

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)))

Page 12: COP4020 Programming Languages Functional Programming Prof. Xin Yuan

COP4020 Spring 2014 1204/21/23

Example 3

Generate a list of n copies of x(define fill (lambda (n x) ??? ))

Page 13: COP4020 Programming Languages Functional Programming Prof. Xin Yuan

COP4020 Spring 2014 1304/21/23

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)

Page 14: COP4020 Programming Languages Functional Programming Prof. Xin Yuan

COP4020 Spring 2014 1404/21/23

Example 4

Replace x with y in list xs(define subst (lambda (x y xs)

???

))

Page 15: COP4020 Programming Languages Functional Programming Prof. Xin Yuan

COP4020 Spring 2014 1504/21/23

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)))) ) ))