Hello
I am trying to learn Lisp.
I wrote a function like this :
(defun topla(deger)
(cond ((<= deger 0) 0)
(T (+ deger (topla (- deger 1))))))
I tried to sum total (my english is inadequate excuse me, you can see
what the code trying to do.)
For example if I give 3-15000 etc. it works. But when I give for
example 25000 it is giving error. It is giving "stack overflow".
How can I make this with no error ? Which controls must be added to
code ? I want to use recursion.
Thanks.
········@gmail.com เขียน:
> Hello
>
> I am trying to learn Lisp.
> I wrote a function like this :
>
> (defun topla(deger)
> (cond ((<= deger 0) 0)
> (T (+ deger (topla (- deger 1))))))
>
> I tried to sum total (my english is inadequate excuse me, you can see
> what the code trying to do.)
>
> For example if I give 3-15000 etc. it works. But when I give for
> example 25000 it is giving error. It is giving "stack overflow".
>
> How can I make this with no error ? Which controls must be added to
> code ? I want to use recursion.
>
> Thanks.
Turn it into tail call recursive, so compiler can optimize it.
(defun sum (i &optional (accum 0))
(if (<= i 0)
accum
(sum (1- i) (+ i accum))))
And don't forget to compile the function with
(compile 'sum)
In CLISP it doesn't work unless I compile the function; the tail call
optimize probably doesn't take place on interpreted function.
········@gmail.com writes:
> Hello
>
> I am trying to learn Lisp.
> I wrote a function like this :
>
> (defun topla(deger)
> (cond ((<= deger 0) 0)
> (T (+ deger (topla (- deger 1))))))
>
> I tried to sum total (my english is inadequate excuse me, you can see
> what the code trying to do.)
>
> For example if I give 3-15000 etc. it works. But when I give for
> example 25000 it is giving error. It is giving "stack overflow".
>
> How can I make this with no error ? Which controls must be added to
> code ? I want to use recursion.
Note some 12-yo child came with this solution two centuries ago:
(defun topla (deger)
(if (<= deger 0)
0
(* 1/2 deger (1+ deger))))
--
__Pascal Bourguignon__ http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him. -- Robert Heinlein
········@gmail.com writes:
> Thanks for solution :)
> I am trying to learn recursive functions.
> When I think for a recursion required problem, I could find this.
There are better examples of recursion.
Working on data structures naturally recursives, such as trees or
graph for examples.
Compute the sum of all numbers in a s-expr:
'(1 (2 "ignore" this (3 (4 5) 6 ((((7))) 8) 9)) 10 11)
(defstruct node label left right) ; a binary tree node.
Given a binary tree BT, build a new super-tree where all the leaves of
BT have two children containing a prefix and a suffix of the label of
the old leaf.
exemple: (split-leaves (make-node :label "Hello World"))
--> #S(node :label "Hello World!"
:left #S(node :label "Hello " :left nil :right nil)
:right #S(node :label "World!" :left nil :right nil))
--
__Pascal Bourguignon__ http://www.informatimago.com/
Cats meow out of angst
"Thumbs! If only we had thumbs!
We could break so much!"
Thanks again.
I tried to learn Lisp from this online Lisp course :
http://www.psychologie.uni-trier.de/projects/ELM/elmart.html
There is many recursive (and tail recursive) examples at there and I
solved more of them (little examples they are.)
I wanted to write a function by myself. But I did wrong and stacks
overflowed :) after than I came here.
Emre Sevinc who writes here often recommends this usenet group to users
who wants to learn Lisp, at their web site and I asked my question
here, I hope I didnt mistake with doing this.
On 18 Oct 2005 10:46:57 -0700, ········@gmail.com wrote:
> Emre Sevinc who writes here often recommends this usenet group to
> users who wants to learn Lisp, at their web site and I asked my
> question here, I hope I didnt mistake with doing this.
No, that was fine. Don't hesitate to come back if you have more
questions.
--
Lisp is not dead, it just smells funny.
Real email: (replace (subseq ·········@agharta.de" 5) "edi")
········@gmail.com writes:
>
> Thanks for heartening :)
>
> (I found "hearten" from dictionary, I hope I didnt use it wrong,
> thanks again to all :)
Well, it's certainly not idiomatic, but I think we caught your drigt.
Your English is better than my Turkish, anyway:-)
--
Robert Uhl <http://public.xdi.org/=ruhl>
The problem with 'post-modern' society is there are too many people with
nothing meaningful to do, building 'careers' around controlling the lives of
others and generally making social nuisances of themselves.
--Graham Strachan
········@gmail.com writes:
> Thanks again.
>
> I tried to learn Lisp from this online Lisp course :
> http://www.psychologie.uni-trier.de/projects/ELM/elmart.html
>
> There is many recursive (and tail recursive) examples at there and I
> solved more of them (little examples they are.)
> I wanted to write a function by myself. But I did wrong and stacks
> overflowed :) after than I came here.
Welcome home.
> Emre Sevinc who writes here often recommends this usenet group to users
> who wants to learn Lisp, at their web site
Yes I do. There are many reasons to be here and I guess
you already saw a few of those reasons.
>and I asked my question
> here, I hope I didnt mistake with doing this.
Try to be careful when you're phrasing your questions
but don't bother yourself too much for the perfect English.
As long as you can get your message across, the rest is
not that important and as far as I can see your English is not bad.
So do not hesitate to write your technical questions
and let the wise people of comp.lang.lisp show you the
way. (Well, always keep copies of HyperSpec, CLtL2, and
books such as Practical Common Lisp and Successful Lisp
around and if you can't get an answer from them or be
confused, then come and ask here).
Another fine resource for daily and simple tasks in
Common Lisp is cl-cookbook:
http://cl-cookbook.sourceforge.net/
Oh, btw (by the way), if you are sensitive about the language
(I mean the natural language) quality of your message
I advise you to use Emacs and GNUS and let them check
your spelling before your post (of course they are
no panacea for syntactic errors, but that's another
problem [hey, anybody knows such software for Emacs?]).
--
Emre Sevinc
eMBA Software Developer Actively engaged in:
http:www.bilgi.edu.tr http://ileriseviye.org
http://www.bilgi.edu.tr http://fazlamesai.net
Cognitive Science Student http://cazci.com
http://www.cogsci.boun.edu.tr
On Tue, 18 Oct 2005 17:14:28 +0200, Pascal Bourguignon <····@mouse-potato.com> wrote:
> Note some 12-yo child came with this solution two centuries ago:
>
> (defun topla (deger)
> (if (<= deger 0)
> 0
> (* 1/2 deger (1+ deger))))
According to various sources the child (Carl Friedrich Gau�) was even
younger (some say 10, some say 9) and the problem was a bit harder:
<http://mathforum.org/social/articles/ross.html>
Cheers,
Edi.
--
Lisp is not dead, it just smells funny.
Real email: (replace (subseq ·········@agharta.de" 5) "edi")
Edi Weitz <········@agharta.de> writes:
> On Tue, 18 Oct 2005 17:14:28 +0200, Pascal Bourguignon <····@mouse-potato.com> wrote:
>
>> Note some 12-yo child came with this solution two centuries ago:
>>
>> (defun topla (deger)
>> (if (<= deger 0)
>> 0
>> (* 1/2 deger (1+ deger))))
>
> According to various sources the child (Carl Friedrich Gau�) was even
> younger (some say 10, some say 9) and the problem was a bit harder:
>
> <http://mathforum.org/social/articles/ross.html>
Are Gauss' school notepads published somewhere? :-)
(defun gauss (min step count)
"
n + n+i + n+2i + n+3i .... + n+pi
= n * (1 + i + 2i + 3i + ... + pi)
= n * (1 + i * (1+2+3+...+p))
= n * (1 + i * p*(p+1)/2)
"
(* min (1+ (* 1/2 step (1- count) count))))
(defun topla (deger) (gauss 1 1 (1+ deger)))
--
"Debugging? Klingons do not debug! Our software does not coddle the
weak."