Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Data type (Shishir Srivastava)
2. Re: Data type (Matthew Moppett)
3. Data type (Shishir Srivastava)
4. Re: Data type (Brandon Allbery)
5. Re: Data type (Shishir Srivastava)
6. Re: Data type (Brandon Allbery)
----------------------------------------------------------------------
Message: 1
Date: Tue, 5 May 2015 16:19:03 +0100
From: Shishir Srivastava <[email protected]>
To: beginners <[email protected]>
Subject: [Haskell-beginners] Data type
Message-ID:
<cale5rtv50qob0q3uy_ukarpfcxnbv4vjp0d7xbmadc6brcj...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150505/495fb13e/attachment-0001.html>
------------------------------
Message: 2
Date: Tue, 5 May 2015 22:23:53 +0700
From: Matthew Moppett <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Data type
Message-ID:
<CAMLEjZDQexg=rdnk9nbodcu_1q5fzv6d3fv+xfojohs4dwa...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
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 <
[email protected]> 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
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150505/80755cee/attachment-0001.html>
------------------------------
Message: 3
Date: Tue, 5 May 2015 16:28:08 +0100
From: Shishir Srivastava <[email protected]>
To: beginners <[email protected]>
Subject: [Haskell-beginners] Data type
Message-ID:
<cale5rtsqgtizvj0putfxhtscgcspuv7oo0k6av-jrbu+qbg...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Thanks Matthew - that was crisp !
Cheers,
Shishir
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150505/f39180cc/attachment-0001.html>
------------------------------
Message: 4
Date: Tue, 5 May 2015 11:28:24 -0400
From: Brandon Allbery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Data type
Message-ID:
<cakfcl4wpd-f8rtm9sjbzghsfzmrdb6xkk8b+dfnermv5y7g...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Tue, May 5, 2015 at 11:19 AM, Shishir Srivastava <
[email protected]> 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
[email protected] [email protected]
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-0001.html>
------------------------------
Message: 5
Date: Tue, 5 May 2015 16:38:11 +0100
From: Shishir Srivastava <[email protected]>
To: beginners <[email protected]>
Subject: Re: [Haskell-beginners] Data type
Message-ID:
<CALe5RTu9ZxePfi4ks6c=3obhajkyyafh5ur7cqlpqheqseq...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hi Brandon,
Thanks for your response. My example was just a bad turnout which I
conjured up using tutorials and playing with it.
I was going to follow up my question with the possible practical use of why
and where someone would use such a construct to wrap a function inside a
new data-type.
For all that matters I could have used 'length' function directly to get
the same output.
I appreciate that you already have given the practical example but anything
more basic for beginners to highlight the usage would be helpful.
Thanks,
Shishir
On Tue, May 5, 2015 at 4:28 PM, Shishir Srivastava <
[email protected]> wrote:
> Thanks Matthew - that was crisp !
>
> Cheers,
> Shishir
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150505/97e25ce6/attachment-0001.html>
------------------------------
Message: 6
Date: Tue, 5 May 2015 11:49:58 -0400
From: Brandon Allbery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Data type
Message-ID:
<cakfcl4w5mmafwl-kjdlh8rabs+7iopaxhr+jksfa1cveqzi...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Tue, May 5, 2015 at 11:38 AM, Shishir Srivastava <
[email protected]> wrote:
> I was going to follow up my question with the possible practical use of
> why and where someone would use such a construct to wrap a function inside
> a new data-type.
>
> For all that matters I could have used 'length' function directly to get
> the same output.
>
Because you want a different function for each value, typically. Continuing
with the DynamicLog example, I have two different uses of DynamicLog in my
xmonad config; one feeds an EWMH panel from the logHook, so it sets
defaultPP { {- ... -}, ppOutput = dbusOutput ch }
where ch was previously associated with a dbus endpoint. This is executed
whenever the focused window changes or the current workspace changes.
The second is in a keybinding I use for debugging and outputs to the
session log (~/.xsession-errors):
defaultPP { {- ... -}, ppOutput = hPutStrLn stderr }
(The ellipsis comments customize the value in other ways not important
here.)
You might think of this use of record syntax as being the Haskell version
of "named parameters" (as distinct from positional parameters) present in
some other languages.
--
brandon s allbery kf8nh sine nomine associates
[email protected] [email protected]
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150505/80fa9e62/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 83, Issue 4
****************************************