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
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
>
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 (
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
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 = [(
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
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
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
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
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
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
11 matches
Mail list logo