Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/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. Re: Can fields in a record be optional? (Mats Rauhala)
2. Re: Can fields in a record be optional? (Antoine Latter)
3. Re: Can fields in a record be optional? (David Place)
4. Re: Can fields in a record be optional? (David Place)
5. Drawing Information from a function already defined
(Clockwork PC)
6. Re: Drawing Information from a function already defined
(john melesky)
7. Re: Drawing Information from a function already defined
(Brent Yorgey)
8. Parse error: naked expression at top level (Roelof Wobben)
----------------------------------------------------------------------
Message: 1
Date: Wed, 20 Jul 2011 15:07:24 +0300
From: Mats Rauhala <[email protected]>
Subject: Re: [Haskell-beginners] Can fields in a record be optional?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
On 18:18 Sun 17 Jul , David Place wrote:
> The way I often do this is to create an "ur" instance where all the fields
> have default values. Then to create an instance, I just do a "record update"
> of this instance. For example:
>
> urContract = Contract { currency = undefined, payments = undefined, contracts
> = [] }
Out of curiosity, what's the etymology for your 'ur'?
--
Mats Rauhala
MasseR
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110720/fa0bdf8e/attachment-0001.pgp>
------------------------------
Message: 2
Date: Wed, 20 Jul 2011 07:35:58 -0500
From: Antoine Latter <[email protected]>
Subject: Re: [Haskell-beginners] Can fields in a record be optional?
To: David Place <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID:
<cakjsnqf59yxwc+abyg9g9y9472weoh8+o_ffe1+_wkcp0mv...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
I would expect I would run into trouble with this approach - when a function
receives one of these records as an argument, how does it know if it is safe
to acces the record fields in question?
I would prefer using Maybe types, different types altogether for summed
contracts, or imaking a Contract a type with two cases (as suggested
earlier).
On Jul 17, 2011 5:19 PM, "David Place" <[email protected]> wrote:
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110720/426b16a6/attachment-0001.htm>
------------------------------
Message: 3
Date: Wed, 20 Jul 2011 08:47:45 -0400
From: David Place <[email protected]>
Subject: Re: [Haskell-beginners] Can fields in a record be optional?
To: Mats Rauhala <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Jul 20, 2011, at 8:07 AM, Mats Rauhala wrote:
> On 18:18 Sun 17 Jul , David Place wrote:
>> The way I often do this is to create an "ur" instance where all the fields
>> have default values. Then to create an instance, I just do a "record
>> update" of this instance. For example:
>>
>> urContract = Contract { currency = undefined, payments = undefined,
>> contracts = [] }
>
> Out of curiosity, what's the etymology for your 'ur'?
A prefix denoting the earliest or original form of something.
>
> --
> Mats Rauhala
> MasseR
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 4
Date: Wed, 20 Jul 2011 08:50:21 -0400
From: David Place <[email protected]>
Subject: Re: [Haskell-beginners] Can fields in a record be optional?
To: Antoine Latter <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
Yes, I agree with you. In trying to attend to one thing at a time, I copied
the use of "undefined" from the original poster's code. Certainly, it makes
it easier to debug code if you can have a Show instance that doesn't throw an
error!
____________________
David Place
Owner, Panpipes Ho! LLC
http://panpipesho.com
[email protected]
On Jul 20, 2011, at 8:35 AM, Antoine Latter wrote:
> I would expect I would run into trouble with this approach - when a function
> receives one of these records as an argument, how does it know if it is safe
> to acces the record fields in question?
>
> I would prefer using Maybe types, different types altogether for summed
> contracts, or imaking a Contract a type with two cases (as suggested earlier).
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110720/6b035e0c/attachment-0001.htm>
------------------------------
Message: 5
Date: Wed, 20 Jul 2011 23:45:52 +1000
From: Clockwork PC <[email protected]>
Subject: [Haskell-beginners] Drawing Information from a function
already defined
To: [email protected]
Message-ID:
<cal1-yvnmd8qpt+1zdeeuxtapw6obxk_bazpmtwokbxrwafp...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Greetings Haskell community,
This is my first ever post so please be gentle...
Here's where I got to:
Jumped into GHCi:
GHCi, version 7.0.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Defined my function:
Prelude> let rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <-
[1..10], a^2 + b^2 == c^2 ]
Tested my function:
Prelude> rightTriangles
[(4,3,5),(3,4,5),(8,6,10),(6,8,10)]
Now, I want to define a refinement of this that will only select values
whose total is 24.
I could write it so:
let rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <- [1..10],
a^2 + b^2 == c^2,* a+b+c == 24*]
However, it seems like a bit of a waste if I already have "rightTriangles"
defined.
I tried a few things, but none of them worked:
Prelude> let rightTriangles` = rightTriangles, a+b+c == 24
<interactive>:1:21: parse error on input `='
Prelude> let rightTriangles` = rightTriangles | a+b+c == 24
<interactive>:1:21: parse error on input `='
Prelude> let rightTriangles` = [rightTriangles, a+b+c == 24]
<interactive>:1:21: parse error on input `='
Prelude> let rightTriangles` = [rightTriangles | a+b+c == 24]
<interactive>:1:21: parse error on input `='
Basically, I'm trying to work out how to draw data from a list already to
hand.
Alexander
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110720/6d10fdf3/attachment-0001.htm>
------------------------------
Message: 6
Date: Wed, 20 Jul 2011 13:57:39 +0000
From: john melesky <[email protected]>
Subject: Re: [Haskell-beginners] Drawing Information from a function
already defined
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Wed, Jul 20, 2011 at 11:45:52PM +1000, Clockwork PC wrote:
> Defined my function:
>
> Prelude> let rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <-
> [1..10], a^2 + b^2 == c^2 ]
>
> ...
>
> Now, I want to define a refinement of this that will only select values
> whose total is 24.
>
> ...
>
> Basically, I'm trying to work out how to draw data from a list already to
> hand.
This last part says it. You already know how to draw data from a list:
you do it above when you pull, e.g., c from [1..10]. You actually do
it three times (for a, b, and c).
This time around, you have an existing list (named rightTriangles),
and you want to pull (a,b,c) from it, rather than pulling a, b, and c
from separate lists. Otherwise, it's much the same as what you were
doing before.
So the structure of it would look something like:
let myTriangles = [ (a,b,c) | (a,b,c) <- rightTriangles, *condition* ]
^ a new name ^ same sort of output ^ from your existing list
where *condition* is your "add up to 24" requirement, in the same form
as the "a^2+b^2==c^2" requirement in your first list comprehension.
Hope that helps.
-john
------------------------------
Message: 7
Date: Wed, 20 Jul 2011 09:59:22 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Drawing Information from a function
already defined
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Wed, Jul 20, 2011 at 11:45:52PM +1000, Clockwork PC wrote:
> Greetings Haskell community,
>
> This is my first ever post so please be gentle...
Hi Alexander, welcome! I hope you will find that people here are
gentle whether it is your first post or 500th.
> Defined my function:
>
> Prelude> let rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <-
> [1..10], a^2 + b^2 == c^2 ]
>
> Tested my function:
>
> Prelude> rightTriangles
> [(4,3,5),(3,4,5),(8,6,10),(6,8,10)]
Looks good.
> Now, I want to define a refinement of this that will only select values
> whose total is 24.
>
> I could write it so:
>
> let rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <- [1..10],
> a^2 + b^2 == c^2,* a+b+c == 24*]
>
> However, it seems like a bit of a waste if I already have "rightTriangles"
> defined.
Indeed! In general Haskell offers lots of abstraction that makes it
easy not to repeat yourself, so this is a good intuition.
> I tried a few things, but none of them worked:
>
> Prelude> let rightTriangles` = rightTriangles, a+b+c == 24
First of all, backtick ` cannot be used as part of an identifier
name. Instead you probably want rightTriangles' (with a single
quote).
The second issue is that there is no way to "extend" a list
comprehension. Once you have defined a list using a list
comprehension, all you have is the list -- there is no longer any way
to "reach inside" the definition and see that it was defined using a
list comprehension, much less extend or alter the definition. So we
have to make do with manipulating the list rightTriangles itself.
First, you could simply write a new list comprehension, pulling values
from the original rightTriangles list, like so:
rightTriangles' = [ (a,b,c) | (a,b,c) <- rightTriangles, a+b+c = 24 ]
Or you could use the 'filter' function. As its first argument it
expects a *function* which returns a Bool (True or False) for each
element in the list, specifying whether to keep it (True) or throw it
away (False). As its second argument it takes the list you want to
filter. So:
rightTriangles' = filter sum24 rightTriangles
where sum24 (a,b,c) = (a+b+c) == 24
Here the sum24 function decides whether to keep each triple (a,b,c):
we only want to keep those whose sum is equal to 24.
-Brent
------------------------------
Message: 8
Date: Wed, 20 Jul 2011 15:30:02 +0000
From: Roelof Wobben <[email protected]>
Subject: [Haskell-beginners] Parse error: naked expression at top
level
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hello,
I made this little script :
replicate :: Int -> a -> [a]
replicate a xs = [xs | x' <= a <--xs]
replicate 3 True
But when I let it run in GHCi, version 7.0.3 on a FreeBSD box I see this error :
oefening.hs:4:1: Parse error: naked expression at top level
Failed, modules loaded: none.
How can I solve this and what does it mean.
Roelof
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110720/31d7518f/attachment.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 37, Issue 38
*****************************************