Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Functional programming Imperative versus functional PL Imperative PL = state based and assignment oriented Functional P = value based and applicative - A functional PL has three primary components: a set of data objects a set of built-in functions a set of functional forms Examples of functional forms: defun - LISP define - Scheme fun - ML function reduction – reduce - APL The execution of functional programs is based on two fundamental mechanisms: - binding - application val sq = fn(x:int) => x * x; fun square(n:int) = n * n; (fn(x:int) => x * x) 2; ((lambda (x y) (+ x y)) 2 3) Free and bound variables in lambda calculus A variable x occurs free in a lambda calculus expression E if and only if: 1. E is a variable reference and E is the same as x or 2. E is of the form (lambda (y) E’) where y is different from x and occurs free in E’ or 3. E is of the form (E1 E2) and x occurs free in E1 or E2 A variable x occurs bound in a lambda calculus expression if and only if 1. E is of the form (lambda (y) E’) where x occurs bound in E’ or x and y are the same variable and y occurs free in E’ or 2. E is of the form (E1 E2) and x occurs bound in E1 or E2 (define-datatype bintree bintree? (leaf (datum number?)) (node (key symbol?) (left bintree?) (right bintree?))) (define leaf-sum (lambda (tree) (cases bintree tree (leaf (datum) datum) (node (key left right) (+ (leaf-sum left) (leaf-sum right))))) General form of case (cases type-name expression (variant-name (field-name*) consequent)* (else default)) (define-datatype expression expression? (var-expr (id symbol?)) (lambda-expr (id symbol?) (body expression?)) (app-expr (rator expression?) (rand expression?))) We can now write functions to test if a variable is free or bound in an expression (define occurs-free? (lambda (var exp) (cases expression exp (var-exp (id) (eqv? id var)) (lambda-exp (id body) (and (not (eqv? id var)) (occurs-free? var body))) (app-expr (rator rand) (or (occurs-free? var rator) (occurs-free? var rand))))) (define occurs-bound? (lambda (var exp) (cases expression exp (lambda-exp (id body) (or (occurs-bound? var body) (and (eqv? id var) (occurs-free? id body)))) (app-expr (rator rand) (or (occurs-bound? var rator) (occurs-bound? var rand)))))) Parse-expression <expression> ::= <identifier> var-expr (id) ::=(lambda (<identifier>) <expression>) lambda-exp (id body) ::= (<expression> <expression>) app-exp (rator rand) (define parse-expression (lambda (datum) (cond ((symbol? datum) (var-expr datum)) ((pair? datum) (if (eqv? (car datum) ‘lambda) (lambda-expr (caadr datum) (parse-expression (caddr datum))) (app-expr (parse-expression (car datum)) (parse-expression (cadr datum))))) (else (eopl:error ‘parse-expression “Invalid syntax ~s” datum)))))