SlideServe
Menu
Préférences
© SlideServe · v2026

Mastering Higher Order Functions

marvin-carney avatar
9 diapositives · 216 Views · Nov 17, 2014

Learn how higher order functions improve code abstraction, making it smaller, reusable, and scalable. Explore eval, function, apply, funcall, mapcar, and lambda in Lisp programming.

Mastering Higher Order Functions - slide 1
01 / 9Mastering Higher Order Functions
1 / 17

Télécharger la présentation

Mastering 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 “I hope you’re convinced, by now, that programming languages with first-class functions let you find more opportunities for abstraction, which means your code is smaller, tighter, more reusable, and more scalable.”—Joel Spolsky

  2. Eval • eval is the heart of REPL • Takes an expression, evaluates it and returns it >(eval '(+ 2 5)) 7 >(eval (list '* 2 5 7)) 70 >(eval (eval '''eval)) EVAL

  3. Function (#') • function returns the object associated with a function name >(function +) #<compiled-function +> >(defun f (x) (* x x)) F >(function f) (LAMBDA-BLOCK F (X) (* X X)) >(function (lambda (x) (* x x))) (LAMBDA-CLOSURE () () () (X) (* X X)) • Exact results depend on implementation (these are GCL) • In most contexts, can be abbreviated with #'

  4. Apply • Takes a function and a list of arguments for it, and returns the result of applying the function to the arguments • Last argument must be a list whose final cdr is nil >(apply #'+ '(2 5 6)) 13 >(apply #'* 2 4 '(3 1)) 24

  5. Funcall • Same as apply, but the arguments don’t need to be in a list >(funcall #'+ 2 3 5 6) 16 >(funcall #'cons 'a 'b) (A . B)

  6. Higher Order Functions • A Higher Order Function is one that has at least one function as an argument • apply and funcall are higher order functions • There are many others • You can also make your own

  7. Mapcar • mapcar is a very useful higher order function • Applies a function to each element of a list, one at a time, and returns the list of results (mapcar function list) >(mapcar #'evenp '(1 2 3 4 5 6 7)) (NIL T NIL T NIL T NIL) >(mapcar #'oddp '(1 2 3 4 5 6 7)) (T NIL T NIL T NIL T)

  8. Define Mapcar (defun my-mapcar (f xs) "Function similar to mapcar" (if (consp xs) (cons (funcall f (car xs)) (my-mapcar f (cdr xs))) nil)) • Actual definition is slightly more complicated

  9. Anonymous Functions • Lambda abstractions are useful in conjunction with higher order functions • Example: the following function subtracts n from each element in a list: >(defun subtract-n (n xs) (mapcar #'(lambda (x) (- x n)) xs)) SUBTRACT-N >(subtract-n 5 '(10 9 8 2 1)) (5 4 3 -3 -4)