On Mon, 27 Jun 2005, Daniel Fischer wrote:
Am Sonntag, 26. Juni 2005 21:02 schrieben Sie:
On Sun, 26 Jun 2005, Daniel Fischer wrote:
m x y = if x==0 then 0 else x*y
Plain
foldr m 1
does fine, in fact much better than
foldl' (*) 1 . upTo (== 0),
both in
Oops, somehow that reply by Scott Turner (which coincidentally
contained the same example) hadn't appeared for me yet :)
Anyway, seconded :)
On 26/06/05, Cale Gibbard [EMAIL PROTECTED] wrote:
Well, the case of the built-in numeric types is somewhat different,
but most functions automatically
Scott Turner wrote:
It's still possible to use fold and get short circuiting with good memory
usage.
upTo pred = foldr (\a - \xs - if pred a then [a] else a:xs) []
prodList = foldl' (*) 1 . upTo (== 0)
It might be considered cheating, but AFAICT the test for ==0 needs to be
separated
Am Sonntag, 26. Juni 2005 06:06 schrieb Scott Turner:
On 2005 June 25 Saturday 17:49, [EMAIL PROTECTED] wrote:
Simplified:
prodList xs = foldl (*) 1 xs
But my original at least made some provision for short circuiting the
whole operation if the list contained a 0. As far as I can
On Sun, 26 Jun 2005, Daniel Fischer wrote:
m x y = if x==0 then 0 else x*y
Plain
foldr m 1
does fine, in fact much better than
foldl' (*) 1 . upTo (== 0),
both in hugs and ghc, regarding speed and memory usage.
E.g.
foldr m 1 [a,b,c]
means
m a (m b (m c 1)))
That is, it is
Some simplifications might help you here...
prodList [] = 1
prodList (0:xs) = 0
prodList (x:xs) = x * prodList xs
Simplified:
prodList xs = foldl (*) 1 xs
But my original at least made some provision for short circuiting the
whole operation if the list contained a 0. As
Well, the case of the built-in numeric types is somewhat different,
but most functions automatically short circuit, since Haskell uses
lazy evaluation.
For instance, it's perfectly okay to define
myAnd = foldr () True
Note that this terminates on infinite lists which contain False as a value:
On 6/23/05, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
getList = do
putStrLn Give me a number (or 0 to stop):
numString - getLine
let num = read numString
if num == 0
then return []
else return (num:getList)
This will give you an error as well. 'num' is of type
--text follows this line--
I'm trying to learn Haskell from YAHT.
My attempt at a solution of Exercise 3.10 is failing with a Type does
not match error. The exercise is to write a function that will read
numbers (one per line) from the command line, until the number 0 is
entered. At this
On Thu, 2005-06-23 at 00:17 -0400, [EMAIL PROTECTED] wrote:
printFact [] = return
printFact (x:xs) = do -- triggers error message
putStrLn (x ++ factorial is ++ fact x)
printFact xs
return
If anyone can explain to me how to fix this error I'd appreciate it.
You forgot to return a
From: Bernard Pope [EMAIL PROTECTED]
Cc: haskell-cafe@haskell.org
On Thu, 2005-06-23 at 00:17 -0400, [EMAIL PROTECTED] wrote:
You forgot to return a value. ...
Much appreciated!
kj
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
11 matches
Mail list logo