0

I am trying to build a data-structure in Haskell which functions can use to avoid re-computing values. For example, say I had the function:

f :: Int -> Int -> Int
f 1 1 == 1
f m n
    | abs m > n = 0
    | OTHERWISE if value of f m n has already been computed by another recursive branch, return that value and add it to the "database"
    | OTHERWISE return f (m-1) (n-1) + f (m - 1) n

I have already looked at memoization, but haven't been able to implement a solution :\

Suggestions? :)

1 Answer 1

5

A great explanation is here.

I love memoize package :)

Example (solving the "A frog is jumping up the staircase..." problem):

import Data.Function.Memoize 

ladder :: Integer -> Integer -> Integer 
ladder n k = g n 
  where g = memoize f 
        f 0 = 1 
        f x = sum [g (x - y) | y <- [1..if x < k then x else k]] 
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @josesuan, thanks, unfortunately this is my problem, I had come across similar examples, but struggling to understand how it actually works. (In particular, the last line)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.