SlideServe
Menu
Préférences
© SlideServe · v2026

Higher-Order Functions

tiara avatar
16 diapositives · 414 Views · Sep 06, 2014

Higher-Order Functions. 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.

Higher-Order Functions - slide 1
01 / 16Higher-Order Functions
1 / 17

Télécharger la présentation

Higher-Order Functions
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Higher-Order Functions L2HOF

  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 ora 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. L2HOF

  3. Nested Functional + Static Scoping fun f x = let val g = fn y => 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) L2HOF

  4. ML function definitions fun elem_to_list [] = [] | elem_to_list (h::t) = [h] :: (elem_to_list t) elem_to_list [“a”] = [[“a”]] fun inc_each_elem [] = [] | inc_each_elem (h::t)= (h+1) :: (inc_each_elem t) inc_each_elem [1,2,3] = [2,3,4] L2HOF

  5. Abstracting Patterns of Recursion fun map f [] = [] | map f (h::t) = (f h)::(map f t) fun elem_to_list x = map (fn x => [x]) x val inc_each_elem = map (fn x => x+1) L2HOF

  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 ... L2HOF

  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 functionalcomplement. • Refer to: CommonLISP vs Scheme L2HOF

  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 => 5+x) • Curried functions are higher-order functions that consume their arguments lazily. • All ML functions are curried ! L2HOF

  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. L2HOF

  10. fun curry f = fn x => (fn y => f(x,y)) fun uncurry f = fn (x,y) => (f x y) curry (uncurry f) = f uncurry (curry f) = f (Higher-order functions) L2HOF

  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 L2HOF

  12. From Spec. to Impl. • Spec: (sqrt x) >= 0 /\ (sqrt x)^2 = x • Computable Spec: (sqrt x) >= 0 /\ abs((sqrt x)^2 - x) < eps • Improving Approximations (Newton’s method): y(n+1) = (y(n) + [x / y(n)]) / 2 L2HOF

  13. fun improve x y = ( y + x / y) / 2.0 ; val eps = 1.0e~5; fun satis x y = abs( y*y - x) < 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; L2HOF

  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. L2HOF

  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 L2HOF

  16. fun len [] = 0 | len (x::xs) = 1 + len xs (define (len xs) (if (null? xs) 0 (+ 1 (len (cdr xs))) ) ) L2HOF