1 / 16

A global environment

A global environment. val a = 15 val b = "foo"; val c = fn (n, p) => p = n * p; val d = [1,2];. Let and local environments. let val a = 20 in hd(d) + a end;. Function expressions generate closures. fun f(x, y) = let fun nested(z) = a + x + z in nested(y) end;.

sera
Download Presentation

A global environment

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A global environment val a = 15 val b = "foo"; val c = fn (n, p) => p = n * p; val d = [1,2];

  2. Let and local environments let val a = 20 in hd(d) + a end;

  3. Function expressions generate closures fun f(x, y) = let fun nested(z) = a + x + z in nested(y) end;

  4. Function application 1 f(2, 10)

  5. Nested aplication let fun nested(z) = a + x + z in nested(y) end

  6. Closures as parameters val a = 15; fun addA(k) = k + a; fun doAction(f, a) = f(a);

  7. Closures as return values fun makeAddX(x) = fn y => x + y; val add5 = makeAddX 5; val elevn = add5 6;

  8. lambda = let let val x = 3; val y = 4; in x + y end;

  9. Scheme • Expressions: atoms or (parenthesized lists) • Evaluator: • Parse an expression • If expression is (bound to) atom, return it • Otherwise, expression is a list with head V: • If V is a special form, do the special form • Otherwise, evaluate elements in tail and apply V to tail.

  10. Scheme examples (define a 15) ; val a = 15 (define d '(1 2)) ; val d = [1, 2] (let* ((a 20)) (+ (car d) a)) let val a = 20 in hd(d) + a; (define (f x y) (let* ((nested (lambda (z) (+ a x z)))) (nested y))) fun f(x, y) = let val nested = fn z => a + x + z; in nested y;

  11. Scheme dynamic typing • Types are "latent" • Types of values are irrelevant (unchecked) until you use them. • Typing is still strong: attempting to perform an invalid operation will raise a runtime type error

  12. Objects are higher-order functions (define (make-student name id-num) (lambda (message) (cond ((equal? message 'getName) name) ((equal? message 'getID) id-num) ((equal? message 'cloneMe) (make-student (name id-num))) (else "INVALID MESSAGE")))) (define alice (make-student "Alice" 123456)) (display (alice 'getName))

  13. Programs as data ; Evaluate list structure as if it were ; a program (eval (list '+ 1 2 3)) ; Single quote recursively prevents ; evaluation of sublists (define mystuff '(* (+ 1 2 3)))

  14. Why functional programming? • Reason 1: influence on other languages • Understand languages of present • Modern languages borrow heavily from ideas invented in functional languages • Be prepared for languages of the future • Not everyone retires by 35 • New languages come along all the time • Learn concepts once, reuse anywhere

  15. Why functional programming? • Reason 2: Functional style enhances clarity (when used appropriately) • Avoid side effects • Prefer return values to output parameters & side effects. Try to preserve "substitutability". Take advantage of constant data. • Use higher-order thinking • parameterize fns with fns for flexibility and reuse • Polymorphic types • Think of operations that are operate over "generic" types.

  16. Why functional programming? • Reason 3: It's cool. • Programming is a medium for expressing ideas • Functional languages are powerful, flexible, and expressive. • You are not just a code monkey.

More Related