2

I was going through the Software Foundations book (http://www.cis.upenn.edu/~bcpierce/sf/Basics.html). In the second paragraph of Introduction, it is mentioned that as functions are treated as first class values, they can be stored in a data structure. A quick Google search for the applications of this turned up acid-state (http://acid-state.seize.it/) which probably uses this for storing functions (please correct me if I am wrong). Are there any other applications in which storing functions as data in data structures is/can be used? Is storing functions as data a common pattern in Haskell? Thanks in advance.

Edit: I am aware of the higher order functions.

4
  • 3
    Hm.. software foundations might be a bit rough if you haven't really played with Haskell much. If you have, then first class functions are everywhere! map, filter, fold, bind, Reader, State, Cont, lenses, traversals! Close your eyes and throw a rock in the Haskell ecosystem and chances are whatever you hit takes advantage of first class functions. Commented Jun 3, 2014 at 3:06
  • 3
    @jozefg The first few Chapters of the book are quite accessible to newbies, they focus more on proofs than functional programming iirc. Commented Jun 3, 2014 at 3:16
  • 3
    Applicatives rely on being able to store functions in data structures, the State type is literally a function stored in a data structure, lenses are some of the more exotic and interesting functions that are stored in structures. Commented Jun 3, 2014 at 3:45
  • 2
    @luqui game a great example when answering a previous question: stackoverflow.com/questions/7787317/list-of-different-types/… Commented Jun 3, 2014 at 5:17

2 Answers 2

3

A simple and easily-used functions-in-data-structure example is that of having a record containing functions (and possibly data).

A good example is in Luke Palmer's blog post Haskell Antipattern: Existential Typeclass where he explains that rather than try to reproduce an OO-like class for widgets, with many instances and then store your UI widgets in an existentially quantified list, you're better off making a record of the actual functions you'll use and returning that instead of retaining your original datatype - once you're existentially quantified, you can't get at the original datatype anyway, so you don't lose anything in expressiveness, but gain a lot in manipulatability. He concludes

Functions are the masters of reuse: when you use an advanced feature, you need a yet more advanced feature to abstract over it (think: classes < existential types < universally quantified constraints < unknown). But all you need to abstract over a function is another function.

This means that we can write higher-order functions to deal with those functions, and pass them around and manipulate them easily, since they're data.

Sign up to request clarification or add additional context in comments.

1 Comment

Somewhat related: I often store partially-applied functions in a data type where it constrains the possible things you can do with it, e.g. I might initially think to store an Int indicating array size in my type Foo; but if all I ever do with that is call newArray I might redefine the type to store newArray size :: a -> IO (Array a) or whatever. That way I can't mix it up with other Ints and you move more logic related to what Foo does into your newFoo function, rather than scattered around your code (I guess this is one of the goals of OOP?)
2

First class functions are extremely useful -- I would contend that their main use case is in higher order functions, which simplify many problems that require more complex patterns in other languages, like Java.

2 Comments

I am wondering about things other than higher order functions. I am aware of them but have only seen them applied to individual functions (map, fold, filter etc.) but not to a data structure of functions (like list made up of functions).
@eswarp25 Look at functions like ap (or <*>), the first parameter is a data structure of functions, often a list. You need to be a bit familiar with monads and applicative functors to see where it's useful.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.