COP 4020 Programming Languages Functional Programming Prof Xin

  • Slides: 15
Download presentation
COP 4020 Programming Languages Functional Programming Prof. Xin Yuan

COP 4020 Programming Languages Functional Programming Prof. Xin Yuan

Topics n n Functional programming with Scheme Learn by examples 12/18/2021 COP 4020 Spring

Topics n n Functional programming with Scheme Learn by examples 12/18/2021 COP 4020 Spring 2014 2

Defining Global Names n A global name is defined with the “define” special form

Defining Global Names n A global name is defined with the “define” special form (define name value) n n 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 12/18/2021 COP 4020 Spring 2014 3

Defining Global Names n A global name defined with a lambda express in essence

Defining Global Names n 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 12/18/2021 COP 4020 Spring 2014 4

Load program from file n n Load function (load filename) load the program from

Load program from file n n 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. 12/18/2021 COP 4020 Spring 2014 5

I/O n (display x) prints value of x and returns an unspecified value (display

I/O n (display x) prints value of x and returns an unspecified value (display "Hello World!") Displays: "Hello World!" ¨ (display (+ 2 3)) Displays: 5 ¨ n n (newline) advances to a new line (read) returns a value from standard input ¨ n (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”). 12/18/2021 COP 4020 Spring 2014 6

I/O with files n n n n n Open file for reading: (open-input-filename) Open

I/O with files n n n n n Open file for reading: (open-input-filename) Open file for writing: (open-output-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 file) Write a scheme object to file: (write object file) Close file: (close-port file) 12/18/2021 COP 4020 Spring 2014 7

Blocks n n (begin x 1 x 2 … xn) sequences a series of

Blocks n n (begin x 1 x 2 … xn) sequences a series of expressions xi, evaluates them, and returns the value of the last one xn Examples: ¨ (begin (display "Hello World!") (newline) ) 12/18/2021 COP 4020 Spring 2014 8

Example 1: define factorial function: fact(n) = 1*2*3*…*n 12/18/2021 COP 4020 Spring 2014 9

Example 1: define factorial function: fact(n) = 1*2*3*…*n 12/18/2021 COP 4020 Spring 2014 9

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

Example 1: define factorial function: fact(n) = 1*2*3*…*n n 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 12/18/2021 COP 4020 Spring 2014 10

Example 2 n n Sum the elements of a list (define sum (lambda (lst)

Example 2 n n 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/18/2021 COP 4020 Spring 2014 11

Example 3 n Generate a list of n copies of x (define fill (lambda

Example 3 n Generate a list of n copies of x (define fill (lambda (n x) ? ? ? ) ) 12/18/2021 COP 4020 Spring 2014 12

Example 3 n n Generate a list of n copies of x (define fill

Example 3 n n 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 (fill 0 a))) (cons a ())) (a a) 12/18/2021 COP 4020 Spring 2014 13

Example 4 n Replace x with y in list xs (define subst (lambda (x

Example 4 n Replace x with y in list xs (define subst (lambda (x y xs) ? ? ? ) ) 12/18/2021 COP 4020 Spring 2014 14

Example 4 n Replace x with y in list xs (define subst (lambda (x

Example 4 n 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)))) ) 12/18/2021 COP 4020 Spring 2014 15