Re: stack overflow

2015-12-13 Thread Sunil S Nandihalli
Thanks Andy for the response. I was able to make it work by modifying the
code in the following way

(let [modulo 10007
  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 
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 <
> 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 10007
>> 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.
>

-- 
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.


Re: stack overflow

2015-12-13 Thread Andy Fingerhut
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 10007
> 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.


Re: stack overflow

2015-12-13 Thread Mark Engelberg
Call your memoized function on values 1 .. n-1 first, before calling it on
n.
On Dec 12, 2015 9:45 PM, "Sunil S Nandihalli" 
wrote:

> a small correction. The function computes the modulo 10007 of the
> answer and not the answer itself. I ran it in the repl.
> Thanks,
> Sunil.
>
> On Sun, Dec 13, 2015 at 11:08 AM, 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 10007
>> 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.


[ANN] modern-cljs second edition

2015-12-13 Thread Mimmo Cosenza
Hi all,
I just published the very first draft of the second edition of the modern-cljs 
series which already includes 15 tutorials.

In the next few weeks I hope to find the time to publish other 4/5 tutorials of 
the series. 

https://github.com/magomimmo/modern-cljs 


This new edition does not use leining/cljsbuild anymore and is now based on the 
boot build tool (I had some fun with it). The series is now using .cljc 
(instead of leining crossover and cljx) and cljs.test (instead of 
clojurescript.test).

Be forgiving for any typos or even bugs in the code, becasue this is a very 
first draft. 

Hope it helps

Mimmo

p.s. I’m not going to update or support anymore the first edition based on 
leaning/cljsbuild. Sorry about that, but I really do not have the time and the 
strenght to do it. 

-- 
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.


Re: stack overflow

2015-12-13 Thread Sunil S Nandihalli
Thanks Mark. That is exactly what I ended up doing.

Regards,
Sunil.

On Sun, Dec 13, 2015 at 8:45 PM, Mark Engelberg 
wrote:

> Call your memoized function on values 1 .. n-1 first, before calling it on
> n.
> On Dec 12, 2015 9:45 PM, "Sunil S Nandihalli" 
> wrote:
>
>> a small correction. The function computes the modulo 10007 of the
>> answer and not the answer itself. I ran it in the repl.
>> Thanks,
>> Sunil.
>>
>> On Sun, Dec 13, 2015 at 11:08 AM, 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 10007
>>> 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.
>

-- 
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.