What are some interesting uses of higher-order functions?

后端 未结 14 566
走了就别回头了
走了就别回头了 2021-01-30 00:55

I\'m currently doing a Functional Programming course and I\'m quite amused by the concept of higher-order functions and functions as first class citizens. However, I can\'t yet

相关标签:
14条回答
  • 2021-01-30 01:32

    Joel Spolsky wrote a famous essay demonstrating how Map-Reduce works using Javascript's higher order functions. A must-read for anyone asking this question.

    0 讨论(0)
  • 2021-01-30 01:33

    There are several examples here: http://www.haskell.org/haskellwiki/Higher_order_function

    I would also recommend this book: http://www.cs.nott.ac.uk/~gmh/book.html which is a great introduction to all of Haskell and covers higher order functions.

    Higher order functions often use an accumulator so can be used when forming a list of elements which conform to a given rule from a larger list.

    0 讨论(0)
  • 2021-01-30 01:35

    I really started to feel the power when I learned a function can be part of a data structure. Here is a "consumer monad" (technobabble: free monad over (i ->)).

    data Coro i a
        = Return a
        | Consume (i -> Coro i a)
    

    So a Coro can either instantly yield a value, or be another Coro depending on some input. For example, this is a Coro Int Int:

    Consume $ \x -> Consume $ \y -> Consume $ \z -> Return (x+y+z)
    

    This consumes three integer inputs and returns their sum. You could also have it behave differently according to the inputs:

    sumStream :: Coro Int Int
    sumStream = Consume (go 0)
        where
        go accum 0 = Return accum
        go accum n = Consume (\x -> go (accum+x) (n-1))
    

    This consumes an Int and then consumes that many more Ints before yielding their sum. This can be thought of as a function that takes arbitrarily many arguments, constructed without any language magic, just higher order functions.

    Functions in data structures are a very powerful tool that was not part of my vocabulary before I started doing Haskell.

    0 讨论(0)
  • 2021-01-30 01:35

    Martín Escardó provides an interesting example of a higher-order function:

    equal :: ((Integer -> Bool) -> Int) -> ((Integer -> Bool) -> Int) -> Bool
    

    Given two functionals f, g :: (Integer -> Bool) -> Int, then equal f g decides if f and g are (extensionally) equal or not, even though f and g don't have a finite domain. In fact, the codomain, Int, can be replaced by any type with a decidable equality.

    The code Escardó gives is written in Haskell, but the same algorithm should work in any functional language.

    You can use the same techniques that Escardó describes to compute definite integrals of any continuous function to arbitrary precision.

    0 讨论(0)
  • 2021-01-30 01:37

    Check out the paper 'Even Higher-Order Functions for Parsing or Why Would Anyone Ever Want To Use a Sixth-Order Function?' by Chris Okasaki. It's written using ML, but the ideas apply equally to Haskell.

    0 讨论(0)
  • 2021-01-30 01:38

    One interesting and slightly crazy thing you can do is simulate an object-oriented system using a function and storing data in the function's scope (i.e. in a closure). It's higher-order in the sense that the object generator function is a function which returns the object (another function).

    My Haskell is rather rusty so I can't easily give you a Haskell example, but here's a simplified Clojure example which hopefully conveys the concept:

    (defn make-object [initial-value]
      (let [data (atom {:value initial-value})]
          (fn [op & args]
            (case op 
              :set (swap! data assoc :value (first args))
              :get (:value @data)))))
    

    Usage:

    (def a (make-object 10))
    
    (a :get)
    => 10
    
    (a :set 40)
    
    (a :get)
    => 40
    

    Same principle would work in Haskell (except that you'd probably need to change the set operation to return a new function since Haskell is purely functional)

    0 讨论(0)
提交回复
热议问题