
Hi, Could anyone please have a look and let me know how you can create the instance of this data type ? --- data Person = Person { firstName ::String -> Int } --- I've seen this type of syntax in a lot of places in haskell code where a new data type is defined in terms of functions rather than concrete data types. I am not trying to achieve anything out of this but purely as an exercise in understanding the record syntax. 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. Thanks, Shishir

Here's a quick example using ghci: Prelude> let a = Person length Prelude> :type a a :: Person Prelude> (firstName a) "Bob" 3 On Tue, May 5, 2015 at 10:19 PM, Shishir Srivastava < shishir.srivastava@gmail.com> wrote:
Hi,
Could anyone please have a look and let me know how you can create the instance of this data type ?
--- data Person = Person { firstName ::String -> Int } ---
I've seen this type of syntax in a lot of places in haskell code where a new data type is defined in terms of functions rather than concrete data types.
I am not trying to achieve anything out of this but purely as an exercise in understanding the record syntax.
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.
Thanks, Shishir
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

On Tue, May 5, 2015 at 11:19 AM, Shishir Srivastava < shishir.srivastava@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:... ) 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@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
participants (3)
-
Brandon Allbery
-
Matthew Moppett
-
Shishir Srivastava