Functions as Values
You’ve learned how to create data types and records that hold regular Haskell values, and build data types like List that hold recursive values. Another type of value you might sometimes want to hold inside of a data type is a function.
Creating a data type that holds a function looks much like creating any other data type. For example, we can make a data type that holds a function from a String to a pair of strings by saying this:
| data StringParser = StringParser (String -> (String, String)) |
More often than not, when you see data types that hold a single function, they’ll be records with a field that gives the function some useful name:
| data StringParser = |
| StringParser { runStringParser :: String |
Get Effective Haskell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.