简体   繁体   中英

Haskell storing functions in data structures - use cases

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.

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.

一流的功能非常有用-我认为它们的主要用例是在更高阶的函数中 ,它可以简化许多问题,这些问题需要使用其他语言(例如Java)编写更复杂的模式。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM