Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP4020 Programming Languages Functional Programming Prof. Xin Yuan.

Similar presentations


Presentation on theme: "COP4020 Programming Languages Functional Programming Prof. Xin Yuan."— Presentation transcript:

1 COP4020 Programming Languages Functional Programming Prof. Xin Yuan

2 COP4020 Spring 2014 2 11/27/2015 Topics Functional programming with Scheme Learn by examples

3 COP4020 Spring 2014 3 11/27/2015 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

4 COP4020 Spring 2014 4 11/27/2015 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 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 5 11/27/2015

6 COP4020 Spring 2014 6 11/27/2015 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: 5 Displays: You guessed it! (read-line) returns the string in a line (without the “\n”).

7 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 7 11/27/2015

8 COP4020 Spring 2014 8 11/27/2015 Blocks (begin x 1 x 2 … x n ) sequences a series of expressions x i, evaluates them, and returns the value of the last one x n Examples:  (begin (display "Hello World!") (newline) )

9 COP4020 Spring 2014 9 11/27/2015 Example 1: define factorial function: fact(n) = 1*2*3*…*n

10 COP4020 Spring 2014 10 11/27/2015 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

11 COP4020 Spring 2014 11 11/27/2015 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)))

12 COP4020 Spring 2014 12 11/27/2015 Example 3 Generate a list of n copies of x (define fill (lambda (n x) ??? ) )

13 COP4020 Spring 2014 13 11/27/2015 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)

14 COP4020 Spring 2014 14 11/27/2015 Example 4 Replace x with y in list xs (define subst (lambda (x y xs) ??? ) )

15 COP4020 Spring 2014 15 11/27/2015 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)))) ) ) )


Download ppt "COP4020 Programming Languages Functional Programming Prof. Xin Yuan."

Similar presentations


Ads by Google