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 <
sunil.nandiha...@gmail.com> 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 clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> 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 clojure+unsubscr...@googlegroups.com.
> 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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
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 clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to