Thanks Andy for the response. I was able to make it work by modifying the
code in the following way
(let [modulo 100000007
trunc #(mod % modulo)
mult-m (comp trunc *)
add-m (comp trunc +)
h (fn [mf l]
(if (<= l 1) 1
(trunc (reduce add-m 0
(map #(mult-m (mf mf (- l % 1)) (mf mf %))
(range l))))))
mf (memoize h)]
(defn num-trees [n]
(doseq [i (range n)] (mf mf i))
(mf mf n)))
I was able to fix the problem by progressively building up the answer
bottom up.
Regards,
Sunil
On Sun, Dec 13, 2015 at 3:03 PM, Andy Fingerhut <[email protected]>
wrote:
> You can increase the maximum stack size in your JVM with command line
> options when you start it.
>
> You can reduce the amount of stack memory required per recursive
> iteration. I don't know if let-bound symbols like modulo, trunc, mult-m,
> and add-m require separate stack space per recursive call in your function
> or not, but if they do, in your function those could easily be made into
> separate functions defined outside of num-trees.
>
> The biggest way to avoid using stack space is to write the code in a way
> that doesn't make recursive calls, but this can often be fairly
> inconvenient for functions that are naturally written recursively.
> Increasing the maximum stack size would be a lot quicker.
>
> Andy
>
> On Sat, Dec 12, 2015 at 9:38 PM, Sunil S Nandihalli <
> [email protected]> wrote:
>
>> Hi Everybody,
>> I wrote the simple function for computing the number of
>> binary-search-trees that can be formed from a sequence of numbers from 1 to
>> N using memoization. I get a stack-overflow for values more than N of 196.
>> Can anybody help me in figuring out how to fix this?
>> Thanks,
>> Sunil.
>>
>> (defn num-trees [n]
>> (let [modulo 100000007
>> trunc #(mod % modulo)
>> mult-m (comp trunc *)
>> add-m (comp trunc +)
>> h (fn [mf l]
>> (if (<= l 1) 1
>> (trunc (reduce add-m 0
>> (map #(mult-m (mf mf (- l % 1)) (mf mf %))
>> (range l))))))
>> mf (memoize h)]
>> (mf mf n)))
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to [email protected]
>> Note that posts from new members are moderated - please be patient with
>> your first post.
>> To unsubscribe from this group, send email to
>> [email protected]
>> For more options, visit this group at
>> http://groups.google.com/group/clojure?hl=en
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.