[Haskell-beginners] data, records and functions
Emanuel Koczwara
poczta at emanuelkoczwara.pl
Thu Feb 21 12:17:28 CET 2013
Hi,
Dnia 2013-02-21, czw o godzinie 02:48 +0000, Peter Hall pisze:
> > It's first time I see function type (and where is definition?) in
> record
> > syntax. Can somebody explain this?
>
>
> There's no definition, it's a parameter to the constructor, so the
> function can be anything. Taking a much simpler example, you'll be
> familiar with, if you do:
>
>
> data Foo a = Foo a
>
>
> then the first argument to the Foo constructor also doesn't have a
> definition. But when you use it to construct a value, then you provide
> one:
>
>
> myFoo = Foo 3
>
>
> Likewise, when you construct an Ord value, you supply a function as
> the value for the 'less' parameter:
>
>
> numOrd = Ord { less = (<) }
>
>
> or you could use a different function for a different purpose:
>
>
> listLengthOrd = Ord { less = \ a b => length a < length b }
>
>
>
>
>
>
> Hope that helps,
>
>
> Peter
Thanks, it is clear now. I was missing that fields can be functions (a
field can hold a function).
-- here second field is a function
data Command a = Command a (a -> a)
-- do something "useful" with that
executeCommand :: Command a -> a
executeCommand (Command param function) = function param
-- test
executeCommand (Command 5 succ) == 6
Thank you, it was so simple :)
Emanuel
More information about the Beginners
mailing list