Brian Hurt wrote:
nth 0 (x:xs) = Some x
nth i (x:xs) = if i 0 then Empty else nth (i-1) xs
nth i [] = Empty
[blows stack on large i]
As other people have pointed out, this is due to laziness. I'd write it
like:
nth 0 (x:_) = Some x
nth i (_:xs) = of i 0 then Empty else (nth $! i-1)
Brian Hulley wrote:
Brian Hurt wrote:
nth 0 (x:xs) = Some x
nth i (x:xs) = if i 0 then Empty else nth (i-1) xs
nth i [] = Empty
[blows stack on large i]
As other people have pointed out, this is due to laziness. I'd write
it like:
nth 0 (x:_) = Some x
nth i (_:xs) = of i 0 then Empty
Brian Hulley wrote:
Brian Hulley wrote:
Brian Hurt wrote:
nth 0 (x:xs) = Some x
nth i (x:xs) = if i 0 then Empty else nth (i-1) xs
nth i [] = Empty
[blows stack on large i]
As other people have pointed out, this is due to laziness. I'd write
it like:
nth 0 (x:_) = Some x
nth i (_:xs)
My apologies for wasting bandwidth on what I'm sure is a stupid newbie
question.
Given:
-- Reimplementing the wheel here, I know
data Option a = Some a | Empty
deriving (Eq,Ord,Show,Read)
nth 0 (x:xs) = Some x
nth i (x:xs) = if i 0 then Empty else nth (i-1) xs
nth i [] = Empty
bhurt:
My apologies for wasting bandwidth on what I'm sure is a stupid newbie
question.
Given:
-- Reimplementing the wheel here, I know
data Option a = Some a | Empty
deriving (Eq,Ord,Show,Read)
nth 0 (x:xs) = Some x
nth i (x:xs) = if i 0 then Empty else nth (i-1) xs
nth
Hi,
In this case, the stack overflow you are seeing is due to laziness not
tail recursion.
Because you never demand the value of any element in the list, Haskell
never bothers to calculate it. So you have a list that looks like:
[ i, i - 1, (i - 1) - 1, ((i - 1) - 1 - 1), .. ]
So, by the
At Fri, 05 Jan 2007 17:58:14 -0800,
Jeremy Shaw wrote:
Because you never demand the value of any element in the list, Haskell
never bothers to calculate it. So you have a list that looks like:
[ i, i - 1, (i - 1) - 1, ((i - 1) - 1 - 1), .. ]
I should clarify that this is the list that
On Fri, Jan 05, 2007 at 08:17:33PM -0500, Brian Hurt wrote:
My apologies for wasting bandwidth on what I'm sure is a stupid newbie
question.
Given:
-- Reimplementing the wheel here, I know
data Option a = Some a | Empty
deriving (Eq,Ord,Show,Read)
My apologies if you knew
On Fri, 5 Jan 2007, Jeremy Shaw wrote:
Hi,
In this case, the stack overflow you are seeing is due to laziness not
tail recursion.
Aha. I knew it was something stupid.
Because you never demand the value of any element in the list, Haskell
never bothers to calculate it. So you have a
On Fri, 5 Jan 2007, Jason Creighton wrote:
On Fri, Jan 05, 2007 at 08:17:33PM -0500, Brian Hurt wrote:
My apologies for wasting bandwidth on what I'm sure is a stupid newbie
question.
Given:
-- Reimplementing the wheel here, I know
data Option a = Some a | Empty
deriving
On Fri, 5 Jan 2007, Jeremy Shaw wrote:
The easiest solution is to make things a little more strict. For
example, if you change:
nth i (x:xs) = if i 0 then Empty else nth (i-1) xs
Even better, if I define:
nth 0 (x:_) = Just x
nth i (_:xs) = if i 0 then Nothing else nth (i-1) xs
nth i []
pps. I didn't explain why [1..100] works, but [1..] fails, because
I don't know :) It's a bit suprising to me...
With [1..100], the generated values have to be tested against
100 as you go. So, they get evaluated. In the [1..] case,
they don't.
-m
At Fri, 05 Jan 2007 20:59:16 -0800,
Mike Gunter wrote:
pps. I didn't explain why [1..100] works, but [1..] fails, because
I don't know :) It's a bit suprising to me...
With [1..100], the generated values have to be tested against
100 as you go. So, they get evaluated.
On 06/01/2007, at 12:58 PM, Jeremy Shaw wrote:
Because you never demand the value of any element in the list, Haskell
never bothers to calculate it. So you have a list that looks like:
[ i, i - 1, (i - 1) - 1, ((i - 1) - 1 - 1), .. ]
So, by the time you get up to some big numbers, you have
Am Samstag, 6. Januar 2007 05:12 schrieb Brian Hurt:
Even better, if I define:
nth 0 (x:_) = Just x
nth i (_:xs) = if i 0 then Nothing else nth (i-1) xs
nth i [] = Nothing
makelist i = i `seq` i : (makelist (i+1))
nth 1000 (makelist 1)
Hi Brian.
i just like to mention another
15 matches
Mail list logo