[Haskell-beginners] Data type

Brandon Allbery allbery.b at gmail.com
Tue May 5 15:28:24 UTC 2015


On Tue, May 5, 2015 at 11:19 AM, Shishir Srivastava <
shishir.srivastava at gmail.com> wrote:

> As far as I understand the data type is function based and takes a
> 'function' instead of a value of a concrete type so how does one create an
> instance of this type.


Functions are first class and fairly concrete (unless polymorphic) in
functional languages. (But I wonder at your example as it seems pretty
strange to have firstName be String -> Int.)

    Person { firstName = \name -> whatever }
    Person { firstName = length }

Practical example from xmonad-contrib:

    logHook = dynamicLogWithPP $ defaultPP { ppOutput = hPutStrLn dock }

(two such fields for the price of one! logHook and ppOutput are both
function-valued) where dock is from a spawnPipe call that launches
something like dzen or xmobar with a pipe connected to its stdin. (See
http://xmonad.org/xmonad-docs/xmonad-contrib/XMonad-Hooks-DynamicLog.html#g:1
)

It is worth remembering lazy evaluation; the functions are not evaluated at
the time of assignment.

-- 
brandon s allbery kf8nh                               sine nomine associates
allbery.b at gmail.com                                  ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150505/c2adbb40/attachment.html>


More information about the Beginners mailing list