Higher-Order Functions - PowerPoint PPT Presentation

About This Presentation
Title:

Higher-Order Functions

Description:

define one generalized functional complement. Refer to: CommonLISP vs Scheme. cs776 (Prasad) ... using the functions supported by the ADT ('no spoofing' ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 17
Provided by: TKPr6
Learn more at: http://cecs.wright.edu
Category:

less

Transcript and Presenter's Notes

Title: Higher-Order Functions


1
Higher-Order Functions
2
Higher-Order Functions
  • A function that takes a function as argument
    and/or returns a function as result is called a
    higher-order function or a functional.
  • ML/Scheme treat functions as first-class
    (primitive) values.
  • Can be supplied as input to functions.
  • Not allowed in Ada.
  • Can be created through expression evaluation.
  • Not allowed in C/Java/LISP.
  • Can be stored in data structures.

3
Nested Functional Static Scoping
  • fun f x
  • let
  • val g fn y gt 8 x y
  • in g
  • end
  • val h f 5
  • h 2
  • Breaks stack-based storage allocation Requires
    heap-based storage allocation and garbage
    collection (Closures)

4
ML function definitions
  • fun elem_to_list
  • elem_to_list (ht)
  • h (elem_to_list t)
  • elem_to_list a a
  • fun inc_each_elem
  • inc_each_elem (ht)
  • (h1) (inc_each_elem t)
  • inc_each_elem 1,2,3 2,3,4

5
Abstracting Patterns of Recursion
  • fun map f
  • map f (ht)
  • (f h)(map f t)
  • fun elem_to_list x
  • map (fn x gt x) x
  • val inc_each_elem
  • map (fn x gt x1)

6
Functionals Reusable modules
  • Libraries usually contain functionals that
    can be customized.
  • sort order list
  • Can be used for
  • descending_order_sort
  • ascending_order_sort
  • sort_on_length
  • sort_lexicographically
  • sort_on_name
  • sort_on_salary
  • ...

7
Orthogonality
  • Instead of defining related but separate
    functions such as
  • remove-if remove-if-not
  • process_till_p process_till_not_p
  • define one generalized functional complement.
  • Refer to CommonLISP vs Scheme

8
Currying
  • fun plus (x, y) x y
  • fun add x y x y
  • plus (5,3) 8
  • add 5 3 8
  • add 5 (fn x gt 5x)
  • Curried functions are higher-order functions that
    consume their arguments lazily.
  • All ML functions are curried !

9
Significance of Curried Functions
  • Supports partial evaluation.
  • Useful for customizing (instantiating) general
    purpose higher-order functions (generics).
  • Succinct definitions.
  • Denotational (Semantics) Specifications.
  • One-argument functions sufficient.
  • All ML functions are unary.

10
  • fun curry f
  • fn x gt (fn y gt f(x,y))
  • fun uncurry f
  • fn (x,y) gt (f x y)
  • curry (uncurry f) f
  • uncurry (curry f) f
  • (Higher-order functions)

11
Classification of functions
  • Primitive
  • , , - , etc.
  • Higher-order
  • Hierarchical
  • (sort order list),(filter pred list)
  • Self-applicable
  • map , id (E.g., (map (map f)) )
  • fun double f f o f

  • composition

12
From Spec. to Impl.
  • Spec
  • (sqrt x) gt 0 /\ (sqrt x)2 x
  • Computable Spec
  • (sqrt x) gt 0 /\
  • abs((sqrt x)2 - x) lt eps
  • Improving Approximations (Newtons method)
  • y(n1) (y(n) x / y(n)) / 2

13
  • fun improve x y
  • ( y x / y) / 2.0
  • val eps 1.0e5
  • fun satis x y
  • abs( yy - x) lt eps
  • fun until p f y
  • if p y then y
  • else until p f (f y)
  • fun sqrt x
  • until (satis x) (improve x) 1.0

14
ML Meta Language
  • Initial Domains of Application
  • Formal Specification of Languages
  • Theorem Proving
  • Theorems are values (terms) of an ADT.
  • Theorems can only be constructed using the
    functions supported by the ADT (no spoofing).
  • ML is a mathematically clean modern programming
    language for the construction of readable and
    reliable programs.
  • Executable Specification.

15
  • Symbolic Values
  • Recursive Definitions
  • Higher-Order Functions
  • Strongly Typed
  • Static Type Inference
  • Polymorphic Type System
  • Automatic Storage Management
  • Pattern Matching
  • ADTs Modules and Functors
  • Functional Imperative features

16
  • fun len 0
  • len (xxs)
  • 1 len xs
  • (define (len xs)
  • (if (null? xs)
  • 0
  • ( 1 (len (cdr xs)))
  • )
  • )
Write a Comment
User Comments (0)
About PowerShow.com