[Haskell-cafe] How to think about this? (profiling)

2008-12-16 Thread Patrick Perry
I agree with everyone else who has said that the better solution is Lemmih's. It is simple, fast, and does not use much memory. On the other hand, here is a more faithful implementation of what you were trying to write. To use mutable arrays, you need to work in either the IO or the ST mo

Re: [Haskell-cafe] How to think about this? (profiling)

2008-12-16 Thread wren ng thornton
Lemmih wrote: On Tue, Dec 16, 2008 at 1:07 PM, Magnus Therning wrote: > I understand your solution, but AFAICS it's geared towards limited > recursion in a sense. What if I want to use memoization to speed up > something like this > > foo :: Int -> Int > foo 0 = 0 > foo 1 = 1 > foo 2 = 2 >

Re: [Haskell-cafe] How to think about this? (profiling)

2008-12-16 Thread Bertram Felgenhauer
Magnus Therning wrote: > This behaviour by Haskell seems to go against my intuition, I'm sure I > just need an update of my intuition ;-) > > I wanted to improve on the following little example code: > > foo :: Int -> Int > foo 0 = 0 > foo 1 = 1 > foo 2 = 2 > foo n = foo (n - 1) + foo (

Re: [Haskell-cafe] How to think about this? (profiling)

2008-12-16 Thread Thorkil Naur
Hello, On Tuesday 16 December 2008 13:19, Felipe Lessa wrote: > 2008/12/16 Magnus Therning : > > That is, where each value depends on _all_ preceding values. AFAIK > > list access is linear, is there a type that is a more suitable state > > for this changed problem? > > > > I realise this particu

Re: [Haskell-cafe] How to think about this? (profiling)

2008-12-16 Thread Emil Axelsson
This is actually a perfect case for lazy immutable arrays, if you use a circular program: import Data.Array foo' :: Array Int Int -> Int -> Int foo' arr 0 = 0 foo' arr 1 = 1 foo' arr 2 = 2 foo' arr n = arr!(n-1) + arr!(n-2) + arr!(n-3) foo :: Int -> Int foo n = arr ! n where assocs = [(

Re: [Haskell-cafe] How to think about this? (profiling)

2008-12-16 Thread Magnus Therning
On Tue, Dec 16, 2008 at 12:14 PM, Lemmih wrote: > You could use a Map or a mutable array. However, this kind of problem > comes up a lot less often than you'd think. Well, I happen to have a problem just like it right now, hence my interest :-) In order to better understand the different option

Re: [Haskell-cafe] How to think about this? (profiling)

2008-12-16 Thread Felipe Lessa
2008/12/16 Magnus Therning : > That is, where each value depends on _all_ preceding values. AFAIK > list access is linear, is there a type that is a more suitable state > for this changed problem? > > I realise this particular function can be written using scanl: [...] > but I guess it's not alway

Re: [Haskell-cafe] How to think about this? (profiling)

2008-12-16 Thread Lemmih
On Tue, Dec 16, 2008 at 1:07 PM, Magnus Therning wrote: > On Mon, Dec 15, 2008 at 11:33 PM, Lemmih wrote: >> 2008/12/16 Magnus Therning : >>> This behaviour by Haskell seems to go against my intuition, I'm sure I >>> just need an update of my intuition ;-) >>> >>> I wanted to improve on the follo

Re: [Haskell-cafe] How to think about this? (profiling)

2008-12-16 Thread Magnus Therning
On Mon, Dec 15, 2008 at 11:33 PM, Lemmih wrote: > 2008/12/16 Magnus Therning : >> This behaviour by Haskell seems to go against my intuition, I'm sure I >> just need an update of my intuition ;-) >> >> I wanted to improve on the following little example code: >> >> foo :: Int -> Int >> foo 0 = 0

Re: [Haskell-cafe] How to think about this? (profiling)

2008-12-15 Thread Lemmih
2008/12/16 Magnus Therning : > This behaviour by Haskell seems to go against my intuition, I'm sure I > just need an update of my intuition ;-) > > I wanted to improve on the following little example code: > > foo :: Int -> Int > foo 0 = 0 > foo 1 = 1 > foo 2 = 2 > foo n = foo (n - 1) + foo (n

[Haskell-cafe] How to think about this? (profiling)

2008-12-15 Thread Magnus Therning
This behaviour by Haskell seems to go against my intuition, I'm sure I just need an update of my intuition ;-) I wanted to improve on the following little example code: foo :: Int -> Int foo 0 = 0 foo 1 = 1 foo 2 = 2 foo n = foo (n - 1) + foo (n - 2) + foo (n - 3) This is obviously goi