Docsity

Functional Programming Lecture: Introduction to Functional Languages and Scheme - Prof. Aa, Study notes of Programming Languages

Partial preview of the text

Download Functional Programming Lecture: Introduction to Functional Languages and Scheme - Prof. Aa and more Study notes Programming Languages in PDF only on Docsity!

Lecture 13: Introduction to Functional Programming COMP 524 Programming Language Concepts Aaron Block

March 1, 2007 Based on notes by N. Fisher, F. Hernandez-Campos, and D. Stotts

Goals • Discuss functional languages

2

No hard dividing line • You can write in a “functional” style in imperative

•^ languages.Most functional languages haver imperatives subsets:

4

(define iter-fib (lambda (n) (do ((i 0 (+ i 1)) ; init 0 (a 0 b) ; init 0, set to b in each iter (b 1 (+ a b))) ; init 1, set to sub of a & b (display b) (display “ ”))))^ ((= i n) b) ;termination test and final value (iter-fib 10)

So Why Functional? • Teaches Truly recursive algorithms

• Easy Polymorphism • How do you write in Java a method that will operate equally properly on reals as well as strings?

• Natural expressiveness for symbolic and algebraic computations • Algorithms vs system Munging

5

History • Lisp

• Common Lisp (CL), Scheme^ •^ Dynamic Scoping

• ML, Haskell, Miranda^ •^ Static scoping

• FP (Backus): Sortof functional APL^ •^ Typing, type inference, pure functional

7

LISP Properties • Homogeneity of programs and data

• Self-Definition^ •^ Programs are lists and a program can examine/change itself^ while running

• Easy to write a Lisp interpreter in Lisp

8

• Book uses Scheme;^ An Overview of Scheme

• Scheme is a particularly elegant Lisp^ •^ We’ll study this and ML

• • Interpreter runs a read-eval-print loopThings typed into the interpreter are evaluated (recursively) once

• • Anything in parentheses is a function call (unless quoted)Parentheses are NOT just grouping, as they are in Algo-family languages

  • Adding a level of parentheses changes meaning. 10

An Overview of Scheme • Scheme:

• • • Boolean valuesNumbersLambda Expressions #t and #f

• Quoting

11

(+ 3 4) => 7 (quote (+ 3 4)) => (+ 3 4) ‘(+ 3 4) =>(+ 3 4)

Conditional • Scheme:

• Conditional expressions

• Imperative stuff • • • Assignmentssequencing (begin)iteration

  • I/O (read, display) 13

(if (< 2 3) 4 5) => 4 (cond ((< 3 2) 1) ((< 4 3) 2) (else 3)) => 3

Binding • You can bind values to names

14

(let ((a 3) (b 4) (square (lambda (x) (* x x))) (plus +)) (sqrt (plus (square a) (square b)))) =>

Binding • Scoping (“all at once”)

16

(letrec ((fact (lambda (n) (if (= n 1) 1 (* n (fact (-n 1)))))) (fact 5))^ ;uses the “inner” fact

Lists and Numbers • The functions car, cdr, and cons allow us to manipulate

lists

17

(car ‘(2 3 4)) => 2 (cdr ‘(2 3 4)) => (3 4) (cdr 2 ‘(3 4)) => (2 3 4) ;Returns the head ;Returns the tail ;Amend the List

Overview of Scheme • We’ll invoke the program by calling a function called ‘simulate’, passing it a DFA

  • description and an input stringThe automaton description is a list of three items: • Start state
  • The transition function is a list of pairs^ •^ •^ Transition functionset of final states
    • • The first element of each pari is a pair, whose first element is a state and whose second element in an input symbolIf the current state and next input symbol match the first element of a pair, then the finite automaton enters the state given by the second element of the pair 19

Overview of Scheme

20

(cons(define simulate^ (lambda (dfa (if (nul? (car dfa) input)^ input) (define move^ (simulate^ (if (infinal?^ (move^ dfa)dfa^ (car‘(accept)^ input))^ ‘(reject))^ (cdr^ input))))) (let^ (lambda (dfa (list (if (eq? ((curstate curstate^ symbol) (car dfa))‘error) (trans (cadr dfa)) (finals (caddr dfa)) (let ((pair trans^ ‘error (if pair (cadr (assoc pair) (list ‘error))) curstate sybol) trans))) finals))))