Re: [Haskell-cafe] Stupid newbie question

2007-01-06 Thread Brian Hulley
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)

Re: [Haskell-cafe] Stupid newbie question

2007-01-06 Thread Brian Hulley
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

Re: [Haskell-cafe] Stupid newbie question

2007-01-06 Thread Brian Hulley
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)

[Haskell-cafe] Stupid newbie question

2007-01-05 Thread Brian Hurt
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

Re: [Haskell-cafe] Stupid newbie question

2007-01-05 Thread Donald Bruce Stewart
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

Re: [Haskell-cafe] Stupid newbie question

2007-01-05 Thread Jeremy Shaw
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

Re: [Haskell-cafe] Stupid newbie question

2007-01-05 Thread Jeremy Shaw
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

Re: [Haskell-cafe] Stupid newbie question

2007-01-05 Thread Jason Creighton
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

Re: [Haskell-cafe] Stupid newbie question

2007-01-05 Thread Brian Hurt
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

Re: [Haskell-cafe] Stupid newbie question

2007-01-05 Thread Brian Hurt
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

Re: [Haskell-cafe] Stupid newbie question

2007-01-05 Thread Brian Hurt
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 []

Re: [Haskell-cafe] Stupid newbie question

2007-01-05 Thread Mike Gunter
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

Re: [Haskell-cafe] Stupid newbie question

2007-01-05 Thread Jeremy Shaw
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.

Re: [Haskell-cafe] Stupid newbie question

2007-01-05 Thread Bernie Pope
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

Re: [Haskell-cafe] Stupid newbie question

2007-01-05 Thread Marc A. Ziegert
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