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: function defenition. Do I understand it right?
(Roelof Wobben)
2. Re: function defenition. Do I understand it right?
(Benjamin Edwards)
3. Re: function defenition. Do I understand it right? (Brent Yorgey)
4. Re: function defenition. Do I understand it right? (Ozgur Akgun)
5. Re: function defenition. Do I understand it right?
(Roelof Wobben)
6. (Integral a) => a vs Integer (Dan Ross)
7. Re: (Integral a) => a vs Integer (Michael Snoyman)
8. Re: (Integral a) => a vs Integer (Dan Ross)
----------------------------------------------------------------------
Message: 1
Date: Tue, 12 Jul 2011 11:43:10 +0000
From: Roelof Wobben <[email protected]>
Subject: Re: [Haskell-beginners] function defenition. Do I understand
it right?
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Oke,
So I changed everything to this :
halve (xs) | null xs = ([],[])
| length xs `mod` 2 == 0 = (take n xs, drop n xs)
| otherwise = (take (n+1) xs, drop (n+1) xs)
where n= length xs `div` 2
main = do
putStrLn $ show $ halve [1,2,3,4]
putStrLn $ show $ halve [1,2,3]
putStrLn $ show $ halve []
But now I see this message :
Error occurred
ERROR line 7 - Unresolved top-level overloading
*** Binding : main
*** Outstanding context : Show b
Roelof
________________________________
> Date: Tue, 12 Jul 2011 10:57:04 +0100
> Subject: Re: [Haskell-beginners] function defenition. Do I understand
> it right?
> From: [email protected]
> To: [email protected]
> CC: [email protected]
>
> Yes, you are totally correct.
>
> your guard for length zero should be ([],[]). The type of the function is
>
> halve :: [a] -> ([a], [a])
>
> so all the code paths have to finish with that type.
>
> Incidentally taking the length of a linked list forces you to walk the
> entire list. You are better off checking as to whether it is empty or
> not.
>
> Regards,
> Ben
>
> On 12 July 2011 10:44, Roelof Wobben
> <[email protected]<mailto:[email protected]>> wrote:
>
> Oke,
>
>
>
> I have now this as function definition.
>
>
>
>
>
> halve (xs) | length xs `mod` 2 == 0 = (take n xs, drop n xs)
> | otherwise = (take (n+1) xs, drop (n+1) xs)
> where n= length xs `div` 2
>
>
> main = do
> putStrLn $ show $ halve [1,2,3,4]
> putStrLn $ show $ halve [1,2,3]
>
>
>
>
> this one works except for empty lists.
>
> So I thought this would work .
>
>
>
> halve (xs) | length xs == 0 = []
>
> | length xs `mod`2 == 0 = (take n xs, drop n xs)
>
> | otherwise = (take (n+1) xs, drop (n+1) xs)
>
> where n = length xs `div`2
>
>
>
>
>
> but then I see this error :
>
>
>
>
>
> Error occurred
> ERROR line 2 - Type error in guarded expression
> *** Term : (take n xs,drop n xs)
> *** Type : ([b],[b])
> *** Does not match : [a]
>
>
>
> So I assume that a function must always have the same output and can't
> have 1 or 2 lists as output.
>
>
>
> Is this the right assumption.
>
>
>
> Roelof
>
>
> ________________________________
> > Date: Tue, 12 Jul 2011 10:34:25 +0100
> > Subject: Re: [Haskell-beginners] function defenition. Do I understand
> > it right?
> > From: [email protected]<mailto:[email protected]>
> > To: [email protected]<mailto:[email protected]>
> > CC: [email protected]<mailto:[email protected]>
> >
> > I don't even understand what you are trying to do :)
> >
> > if you want to pattern match on the empty list
> >
> > foo :: [a] -> [a]
> > foo [] = 0
> > foo (x:xs) = undefined
> >
> > if you want to use the guard syntax
> >
> > foo xs | null xs = 0
> > | otherwise = undefined
> >
> >
> > Ben
> >
> > On 12 July 2011 10:02, Roelof Wobben
> >
> <[email protected]<mailto:[email protected]><mailto:[email protected]<mailto:[email protected]>>>
>
> wrote:
> >
> >
> >
> > hello
> >
> >
> >
> > Everyone thanks for the help.
> >
> > I'm now trying to make this work on a empty list.
> >
> >
> >
> > But my question is.
> >
> > When the definition is :
> >
> >
> >
> > [a] -> [a] [a]
> >
> >
> >
> > Is it correct that I don't can use.
> >
> >
> >
> > length xs = 0 | []
> >
> >
> >
> > Roelof
> >
> > ----------------------------------------
> > > Subject: Re: [Haskell-beginners] function defenition. Do I understand
> > it right?
> > > From:
> [email protected]<mailto:[email protected]><mailto:[email protected]<mailto:[email protected]>>
>
> > > Date: Mon, 11 Jul 2011 18:56:42 -0400
> > > CC:
> [email protected]<mailto:[email protected]><mailto:[email protected]<mailto:[email protected]>>
>
> > > To:
> [email protected]<mailto:[email protected]><mailto:[email protected]<mailto:[email protected]>>
>
> > >
> > > On Jul 11, 2011, at 5:13 PM, Roelof Wobben wrote:
> > >
> > > > What I try to achieve is this:
> > > >
> > > >
> > > >
> > > > [1,2,3,4] will be [1,2] [3,4]
> > > >
> > > > [1,2,3,4,5] will be [1,2,3] [3,4,5]
> > >
> > > So, I think what you want is this http://codepad.org/kjpbtLfR
> > >
> > > Is that correct?
> > _______________________________________________
> > Beginners mailing list
> >
> [email protected]<mailto:[email protected]><mailto:[email protected]<mailto:[email protected]>>
>
> > http://www.haskell.org/mailman/listinfo/beginners
> >
> _______________________________________________
> Beginners mailing list
> [email protected]<mailto:[email protected]>
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 2
Date: Tue, 12 Jul 2011 13:06:22 +0100
From: Benjamin Edwards <[email protected]>
Subject: Re: [Haskell-beginners] function defenition. Do I understand
it right?
To: Roelof Wobben <[email protected]>
Cc: [email protected]
Message-ID:
<can6k4ngfdsqo85zzwcak8cmkeaeozekwzwyoy-v600zb3yk...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
I am not sure where your error is coming from.
module Main
? where
halve :: [a] -> ([a],[a])
halve xs | null xs = ([],[])
? ? ? ? ?| otherwise = (take r xs, drop s xs)
?where (p,q) = divMod (length xs) 2
? ? ? ?(r,s) = if q == 0 then (p,p) else (p + 1,p)
works fine for me in ghci and is approximately what you wrote :) For
what it's worth your implementation has a bug in it, you only want to
take an extra item in the odd case.
Ben
------------------------------
Message: 3
Date: Tue, 12 Jul 2011 09:15:45 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] function defenition. Do I understand
it right?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Tue, Jul 12, 2011 at 11:43:10AM +0000, Roelof Wobben wrote:
>
>
> Oke,
>
>
>
> So I changed everything to this :
>
>
>
>
> halve (xs) | null xs = ([],[])
You do not need this case. ([], []) is what halve [] already would
have returned even without this case: length [] == 0, so it would
evaluate to (take 0 [], drop 0 []) which is ([], []).
> | length xs `mod` 2 == 0 = (take n xs, drop n xs)
> | otherwise = (take (n+1) xs, drop (n+1) xs)
> where n= length xs `div` 2
>
>
> main = do
> putStrLn $ show $ halve [1,2,3,4]
> putStrLn $ show $ halve [1,2,3]
> putStrLn $ show $ halve []
>
>
>
> But now I see this message :
>
>
>
> Error occurred
> ERROR line 7 - Unresolved top-level overloading
> *** Binding : main
> *** Outstanding context : Show b
This message is just because it cannot figure out the type of [] in
'putStrLn $ show $ halve []'. You can write
putStrLn $ show $ halve ([] :: [Int])
to give it an explicit type. It's a bit annoying since we happen to
know that the type of the list makes no difference, but the compiler
can't figure that out.
-Brent
------------------------------
Message: 4
Date: Tue, 12 Jul 2011 17:05:11 +0300
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] function defenition. Do I understand
it right?
To: Brent Yorgey <[email protected]>
Cc: [email protected]
Message-ID:
<calzazpbquzo6voeayawu4e9bx7_fs7pfuch0nr4xy+bpnuv...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hi,
On 12 July 2011 16:15, Brent Yorgey <[email protected]> wrote:
> This message is just because it cannot figure out the type of [] in
> 'putStrLn $ show $ halve []'. You can write
>
> putStrLn $ show $ halve ([] :: [Int])
>
> to give it an explicit type. It's a bit annoying since we happen to
> know that the type of the list makes no difference, but the compiler
> can't figure that out.
>
Actually, the type of the list does make a difference due to different show
instances. Try:
putStrLn $ show $ halve ([] :: [Char])
Cheers,
Ozgur
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110712/29ef43c8/attachment-0001.htm>
------------------------------
Message: 5
Date: Tue, 12 Jul 2011 15:41:26 +0000
From: Roelof Wobben <[email protected]>
Subject: Re: [Haskell-beginners] function defenition. Do I understand
it right?
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
----------------------------------------
> Date: Tue, 12 Jul 2011 09:15:45 -0400
> From: [email protected]
> To: [email protected]
> Subject: Re: [Haskell-beginners] function defenition. Do I understand it
> right?
>
> On Tue, Jul 12, 2011 at 11:43:10AM +0000, Roelof Wobben wrote:
> >
> >
> > Oke,
> >
> >
> >
> > So I changed everything to this :
> >
> >
> >
> >
> > halve (xs) | null xs = ([],[])
>
> You do not need this case. ([], []) is what halve [] already would
> have returned even without this case: length [] == 0, so it would
> evaluate to (take 0 [], drop 0 []) which is ([], []).
>
> > | length xs `mod` 2 == 0 = (take n xs, drop n xs)
> > | otherwise = (take (n+1) xs, drop (n+1) xs)
> > where n= length xs `div` 2
> >
> >
> > main = do
> > putStrLn $ show $ halve [1,2,3,4]
> > putStrLn $ show $ halve [1,2,3]
> > putStrLn $ show $ halve []
> >
> >
> >
> > But now I see this message :
> >
> >
> >
> > Error occurred
> > ERROR line 7 - Unresolved top-level overloading
> > *** Binding : main
> > *** Outstanding context : Show b
>
> This message is just because it cannot figure out the type of [] in
> 'putStrLn $ show $ halve []'. You can write
>
> putStrLn $ show $ halve ([] :: [Int])
>
> to give it an explicit type. It's a bit annoying since we happen to
> know that the type of the list makes no difference, but the compiler
> can't figure that out.
>
> -Brent
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
Hello,
Problem solved. See this url: http://codepad.org/jMPCO1UE
I have tested the difference with [int] and [Char].
With Int You get this output ([],[]) and with Char this one ["",""]
Everyone thanks for the help and patience.
Roelof
------------------------------
Message: 6
Date: Tue, 12 Jul 2011 11:20:57 -0500
From: Dan Ross <[email protected]>
Subject: [Haskell-beginners] (Integral a) => a vs Integer
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
Hi all-
I'm going through Learn You a Haskell for Great Good and I don't
understand the advantage of using (Integral a)=> a vs just Integer as I
show below.
Could someone explain this to me?
Thanks,
Dan
lucky :: (Integral a) => a -> String
lucky 7 = "LUCKY NUMBER SEVEN!"
lucky x = "Sorry, you're out of luck, pal!"
lucky :: Integer -> String
lucky 7 = "LUCKY NUMBER SEVEN!"
lucky x = "Sorry, you're out of luck, pal!"
------------------------------
Message: 7
Date: Tue, 12 Jul 2011 19:22:36 +0300
From: Michael Snoyman <[email protected]>
Subject: Re: [Haskell-beginners] (Integral a) => a vs Integer
To: [email protected]
Cc: [email protected]
Message-ID:
<caka2jgjx12gj6j_4v+-y4a4q9hczgo3igaqt_zfjbzews04...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
On Tue, Jul 12, 2011 at 7:20 PM, Dan Ross <[email protected]> wrote:
> Hi all-
>
> I'm going through Learn You a Haskell for Great Good and I don't understand
> the advantage of using (Integral a)=> a vs just Integer as I show below.
>
> Could someone explain this to me?
>
> Thanks,
>
> Dan
>
> lucky :: (Integral a) => a -> String
> lucky 7 = "LUCKY NUMBER SEVEN!"
> lucky x = "Sorry, you're out of luck, pal!"
>
> lucky :: Integer -> String
> lucky 7 = "LUCKY NUMBER SEVEN!"
> lucky x = "Sorry, you're out of luck, pal!"
Hi Dan,
The first version is polymorphic, and will work on *any* instance of
Integral. This would allow people to use Int, Int32, Int64, etc. The
second version requires the user to pass in specifically an Integer.
Michael
------------------------------
Message: 8
Date: Tue, 12 Jul 2011 11:44:31 -0500
From: Dan Ross <[email protected]>
Subject: Re: [Haskell-beginners] (Integral a) => a vs Integer
To: Michael Snoyman <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
Ah. Because Integer is an instance of Integral right?
So using Integer would be more restrictive.
On Tue, 12 Jul 2011 19:22:36 +0300, Michael Snoyman wrote:
> On Tue, Jul 12, 2011 at 7:20 PM, Dan Ross <[email protected]>
> wrote:
>> Hi all-
>>
>> I'm going through Learn You a Haskell for Great Good and I don't
>> understand
>> the advantage of using (Integral a)=> a vs just Integer as I show
>> below.
>>
>> Could someone explain this to me?
>>
>> Thanks,
>>
>> Dan
>>
>> lucky :: (Integral a) => a -> String
>> lucky 7 = "LUCKY NUMBER SEVEN!"
>> lucky x = "Sorry, you're out of luck, pal!"
>>
>> lucky :: Integer -> String
>> lucky 7 = "LUCKY NUMBER SEVEN!"
>> lucky x = "Sorry, you're out of luck, pal!"
>
> Hi Dan,
>
> The first version is polymorphic, and will work on *any* instance of
> Integral. This would allow people to use Int, Int32, Int64, etc. The
> second version requires the user to pass in specifically an Integer.
>
> Michael
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 37, Issue 21
*****************************************