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: Problems when trying to solve yaht exercise 3.10
(Andrew Wagner)
2. Re: Problems when trying to solve yaht exercise 3.10
(Thomas Davie)
3. Re: Problems when trying to solve yaht exercise 3.10 (ZelluX)
4. Re: infix functions with 3 args (Brandon S. Allbery KF8NH)
5. Fractional Int (Zachary Turner)
6. Fwd: [Haskell-beginners] Fractional Int (Sean Bartell)
----------------------------------------------------------------------
Message: 1
Date: Fri, 20 Mar 2009 08:57:14 -0400
From: Andrew Wagner <[email protected]>
Subject: Re: [Haskell-beginners] Problems when trying to solve yaht
exercise 3.10
To: ZelluX <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Ah, you came very close to getting this right. The problem is on your two
putStrLn lines. For example, putStrLn "The sum is " ++ show(sum)
This parses as (putStrLn "The sum is ") ++ show(sum). Thus, it's trying to
use ++ on an IO () and a string. This obviously won't work. You need one of
the following:
putStrLn ("The sum is " ++ show(sum))
putStrLn $ "The sum is " ++ show(sum)
Ditto for the product line. Also, if I can make 2 other quick comments:
product and sum are already defined in the prelude, so there's no need to
define them here. You should also get used to putting type signatures on
your functions, because it's a good practice. Otherwise, good job!
2009/3/20 ZelluX <[email protected]>
> Hi, all
>
> I'm new to haskell and currently reading yaht. I find some problems when
> trying to solve exercise 3.10.
>
> The exersices asks to read a list of numbers terminated by a zero, and
> figure out the sum and product of the list. My program is as follows:
>
> ex3_10 = do
> hSetBuffering stdin LineBuffering
> numbers <- getNumber
> let sum = foldr (+) 0 numbers
> product = foldr (*) 1 numbers
> putStrLn "The sum is " ++ show(sum)
> putStrLn "The product is " ++ show(product)
>
> getNumber = do
> putStrLn "Give me a number (or 0 to stop):"
> num <- getLine
> if read num == 0
> then return []
> else do
> rest <- getNumber
> return (read num : rest)
>
> But when i load the program, ghci reports error:
> Couldn't match expected type `[a]' against inferred type `IO ()'
> In the first argument of `(++)', namely `putStrLn "The sum is "'
> In a stmt of a 'do' expression:
> putStrLn "The sum is " ++ show (sum)
>
> And i just don't understand the first sentence. Could you tell what does it
> mean?
>
> Thanks for your reply
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20090320/97683cc6/attachment-0001.htm
------------------------------
Message: 2
Date: Fri, 20 Mar 2009 14:00:09 +0100
From: Thomas Davie <[email protected]>
Subject: Re: [Haskell-beginners] Problems when trying to solve yaht
exercise 3.10
To: ZelluX <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes
On 20 Mar 2009, at 13:45, ZelluX wrote:
> Hi, all
>
> I'm new to haskell and currently reading yaht. I find some problems
> when trying to solve exercise 3.10.
>
> The exersices asks to read a list of numbers terminated by a zero,
> and figure out the sum and product of the list. My program is as
> follows:
>
> ex3_10 = do
> hSetBuffering stdin LineBuffering
> numbers <- getNumber
> let sum = foldr (+) 0 numbers
> product = foldr (*) 1 numbers
> putStrLn "The sum is " ++ show(sum)
> putStrLn "The product is " ++ show(product)
>
> getNumber = do
> putStrLn "Give me a number (or 0 to stop):"
> num <- getLine
> if read num == 0
> then return []
> else do
> rest <- getNumber
> return (read num : rest)
>
> But when i load the program, ghci reports error:
> Couldn't match expected type `[a]' against inferred type `IO ()'
> In the first argument of `(++)', namely `putStrLn "The sum is "'
> In a stmt of a 'do' expression:
> putStrLn "The sum is " ++ show (sum)
>
> And i just don't understand the first sentence. Could you tell what
> does it mean?
It means that you can't use ++ on an IO action, ++'s type reveals why:
(++) :: [a] -> [a] -> [a]
It accepts two lists, not a list and an IO action. So the question
then is, which IO action are you using ++ on? The answer is (putStrLn
"The sum is"). Your last two statements are being parsed as:
(putStrLn "The sum is ") ++ (show sum)
(putStrLn "The product is ") ++ (show product)
As a random aside, it's usually a good plan in Haskell to get out of
IO based computations as fast as possible, and use pure functions
instead. You might want to consider something like this:
ex3_10 = do
hSetLineBuffering stdin LineBuffering
interact sumAndProduct
sumAndProduct :: String -> String
sumAndProduct = (\ns -> sumText ns ++ productText ns) . map read . lines
sumText :: (Num a, Show a) => [a] -> String
sumText = ("The sum is " ++) . show . sum
productText :: (Num a, Show a) => [a] -> String
productText = ("The product is " ++) . show . product
Why might you want to do this? Well, firstly, the code becomes more
readable, secondly, a computation that was not sequential is no longer
described as being sequential, and finally, the results are more
composible. We can now use sumText and productText, safe in the
knowledge that they will never have side effects etc.
Bob
------------------------------
Message: 3
Date: Fri, 20 Mar 2009 21:05:00 +0800
From: ZelluX <[email protected]>
Subject: Re: [Haskell-beginners] Problems when trying to solve yaht
exercise 3.10
To: Thomas Davie <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Got it. Many thanks to Thomas Davie's and Andrew Wagner's replies. ^_^
On Fri, Mar 20, 2009 at 9:00 PM, Thomas Davie <[email protected]> wrote:
>
> On 20 Mar 2009, at 13:45, ZelluX wrote:
>
> Hi, all
>>
>> I'm new to haskell and currently reading yaht. I find some problems when
>> trying to solve exercise 3.10.
>>
>> The exersices asks to read a list of numbers terminated by a zero, and
>> figure out the sum and product of the list. My program is as follows:
>>
>> ex3_10 = do
>> hSetBuffering stdin LineBuffering
>> numbers <- getNumber
>> let sum = foldr (+) 0 numbers
>> product = foldr (*) 1 numbers
>> putStrLn "The sum is " ++ show(sum)
>> putStrLn "The product is " ++ show(product)
>>
>> getNumber = do
>> putStrLn "Give me a number (or 0 to stop):"
>> num <- getLine
>> if read num == 0
>> then return []
>> else do
>> rest <- getNumber
>> return (read num : rest)
>>
>> But when i load the program, ghci reports error:
>> Couldn't match expected type `[a]' against inferred type `IO ()'
>> In the first argument of `(++)', namely `putStrLn "The sum is "'
>> In a stmt of a 'do' expression:
>> putStrLn "The sum is " ++ show (sum)
>>
>> And i just don't understand the first sentence. Could you tell what does
>> it mean?
>>
>
> It means that you can't use ++ on an IO action, ++'s type reveals why:
>
> (++) :: [a] -> [a] -> [a]
>
> It accepts two lists, not a list and an IO action. So the question then
> is, which IO action are you using ++ on? The answer is (putStrLn "The sum
> is"). Your last two statements are being parsed as:
>
> (putStrLn "The sum is ") ++ (show sum)
> (putStrLn "The product is ") ++ (show product)
>
> As a random aside, it's usually a good plan in Haskell to get out of IO
> based computations as fast as possible, and use pure functions instead. You
> might want to consider something like this:
>
> ex3_10 = do
> hSetLineBuffering stdin LineBuffering
> interact sumAndProduct
>
> sumAndProduct :: String -> String
> sumAndProduct = (\ns -> sumText ns ++ productText ns) . map read . lines
>
> sumText :: (Num a, Show a) => [a] -> String
> sumText = ("The sum is " ++) . show . sum
>
> productText :: (Num a, Show a) => [a] -> String
> productText = ("The product is " ++) . show . product
>
> Why might you want to do this? Well, firstly, the code becomes more
> readable, secondly, a computation that was not sequential is no longer
> described as being sequential, and finally, the results are more composible.
> We can now use sumText and productText, safe in the knowledge that they
> will never have side effects etc.
>
> Bob
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20090320/95013883/attachment-0001.htm
------------------------------
Message: 4
Date: Fri, 20 Mar 2009 11:25:30 -0400
From: "Brandon S. Allbery KF8NH" <[email protected]>
Subject: Re: [Haskell-beginners] infix functions with 3 args
To: 7stud <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
On 2009 Mar 20, at 7:05, 7stud wrote:
> func2 x y = x + y
>
> func3 x y z = x + y + z
>
> Here are the results:
>
> *Main> func2 10 20
> 30
> *Main> 10 `func2` 20
> 30
> *Main> func3 10 20 30
> 60
> *Main> 10 `func3` 20 30
>
> <interactive>:1:11:
> No instance for (Num (t1 -> t))
> arising from the literal `20' at <interactive>:1:11-15
Keep in mind that any function taking multiple arguments is
indistinguishable from a function taking a single argument and
returning a function that takes more arguments. So we have in this
case "10 `func3` 20" which requires another argument. But because of
Haskell evaluation rules, writing "10 `func3` 20 30" causes Haskell to
try to evaluate "20 30" as a function (i.e. Haskell infers "10 `func3`
(20 30)". We need to tell Haskell not to do this:
(10 `func3` 20) 30
It can easily be seen that "(10 `func3` 20)" returns a function which
is applied to "30", as we intended.
(hm, I'm starting to sound like Oleg. not necessarily a good thing...
this is not a research paper :)
--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [email protected]
system administrator [openafs,heimdal,too many hats] [email protected]
electrical and computer engineering, carnegie mellon university KF8NH
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url :
http://www.haskell.org/pipermail/beginners/attachments/20090320/e1b573c4/PGP-0001.bin
------------------------------
Message: 5
Date: Fri, 20 Mar 2009 15:51:11 -0500
From: Zachary Turner <[email protected]>
Subject: [Haskell-beginners] Fractional Int
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Why is there no instance of Fractional Int or Fractional Integer? Obviously
integers are fractions with denominator 1. I was just doing some basic
stuff to get more familiar with Haskell, and was seriously racking my brain
trying to figure out why the following wouldn't work:
intToString :: Int -> [Char]
intToString n | n<10 = chr (n + (ord '0')):[]
intToString n =
let q = truncate (n/10)
r = n `mod` 10
o = ord '0'
ch = chr (r + o)
in ch:(intToString q)
(yes, this ends up converting the string in reverse, but that's another
issue :P)
I later realized that I could use members of the Integral typeclass such as
divMod, mod, etc to make this better, but nonetheless, why should
truncate(n/10) be invalid, when n is an Int? changing it to
truncate((toRational n)/10) works, but I would expect Integers to already be
rational.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20090320/0a0afdb1/attachment-0001.htm
------------------------------
Message: 6
Date: Fri, 20 Mar 2009 18:01:22 -0400
From: Sean Bartell <[email protected]>
Subject: Fwd: [Haskell-beginners] Fractional Int
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
Sorry, forgot to reply to all.
---------- Forwarded message ----------
From: Sean Bartell <[email protected]>
Date: Fri, Mar 20, 2009 at 5:58 PM
Subject: Re: [Haskell-beginners] Fractional Int
To: Zachary Turner <[email protected]>
For a type "a" to be Fractional requires there to be:
(/) :: a -> a -> a
You can't divide an Int by another Int and (in general) get a third
Int. You would probably want something like a "Fractionable"
typeclass, with
(/) :: a -> a -> b
which would result in a Rational, but Haskell doesn't have this.
2009/3/20 Zachary Turner <[email protected]>
>
> Why is there no instance of Fractional Int or Fractional Integer? Obviously
> integers are fractions with denominator 1. I was just doing some basic
> stuff to get more familiar with Haskell, and was seriously racking my brain
> trying to figure out why the following wouldn't work:
>
> intToString :: Int -> [Char]
> intToString n | n<10 = chr (n + (ord '0')):[]
> intToString n =
> Â Â Â let q = truncate (n/10)
> Â Â Â Â Â Â Â r = n `mod` 10
> Â Â Â Â Â Â Â o = ord '0'
> Â Â Â Â Â Â Â ch = chr (r + o)
> Â Â Â in ch:(intToString q)
>
> (yes, this ends up converting the string in reverse, but that's another issue
> :P)
>
> I later realized that I could use members of the Integral typeclass such as
> divMod, mod, etc to make this better, but nonetheless, why should
> truncate(n/10) be invalid, when n is an Int? changing it to
> truncate((toRational n)/10) works, but I would expect Integers to already be
> rational.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 9, Issue 24
****************************************