From: ········@gmail.com
Subject: What is wrong
Date: 
Message-ID: <1129615530.684145.40110@o13g2000cwo.googlegroups.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.

From: Pisin Bootvong
Subject: Re: What is wrong
Date: 
Message-ID: <1129616954.609593.58710@f14g2000cwb.googlegroups.com>
········@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.
From: ········@gmail.com
Subject: Re: What is wrong
Date: 
Message-ID: <1129618141.080769.103560@g43g2000cwa.googlegroups.com>
Thanks very much for solution and help.
From: Pascal Bourguignon
Subject: Re: What is wrong
Date: 
Message-ID: <87ek6i97sb.fsf@thalassa.informatimago.com>
········@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
From: ········@gmail.com
Subject: Re: What is wrong
Date: 
Message-ID: <1129652069.879192.265140@g43g2000cwa.googlegroups.com>
Thanks for solution :)
I am trying to learn recursive functions.
When I think for a recursion required problem, I could find this.
From: Pascal Bourguignon
Subject: Re: What is wrong
Date: 
Message-ID: <87hdbe7o8a.fsf@thalassa.informatimago.com>
········@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!"
From: ········@gmail.com
Subject: Re: What is wrong
Date: 
Message-ID: <1129657617.494846.151120@o13g2000cwo.googlegroups.com>
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.
From: Edi Weitz
Subject: Re: What is wrong
Date: 
Message-ID: <uy84q8yp6.fsf@agharta.de>
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")
From: ········@gmail.com
Subject: Re: What is wrong
Date: 
Message-ID: <1129660936.960038.302610@g49g2000cwa.googlegroups.com>
Thanks for heartening :)

(I found "hearten" from dictionary, I hope I didnt use it wrong, thanks
again to all :)
From: Robert Uhl
Subject: Re: What is wrong
Date: 
Message-ID: <m3y84pux58.fsf@4dv.net>
········@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
From: Emre Sevinc
Subject: Re: What is wrong
Date: 
Message-ID: <87fyqy1v1k.fsf@ileriseviye.org>
········@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
From: ········@gmail.com
Subject: Re: What is wrong
Date: 
Message-ID: <1129666467.009489.138680@g14g2000cwa.googlegroups.com>
Thanks :)
I will try to be more carefull (unfortunately I am a carefullness
defective person, I hope I do not annoy group members.)
From: ··················@gmail.com
Subject: Re: What is wrong
Date: 
Message-ID: <1129691115.367879.241660@f14g2000cwb.googlegroups.com>
I'm a lisp newbie too. I'm not sure if I understood the second problem
well, but here is my solution for it.

(defstruct node label left right)

(defun split-leaves (node)
  (let* ((the-label (node-label node))
	 (index (search-mid " " the-label)))
    (cond
     ((null index) node)
     (t
      (let ((left-str (subseq the-label 0 index))
	    (right-str (subseq the-label (1+ index))))
	  (make-node :label the-label
		     :left (split-leaves (make-node :label left-str))
		     :right (split-leaves (make-node :label right-str))))))))

(defun search-all (sub str &optional (start 0))
  (let ((found (search sub str :start2 start)))
    (cond
     ((null found) nil)
     (t (cons found (search-all sub str (+ found (length sub))))))))

(defun mid (list)
  (let ((len (length list)))
    (cond
     ((= len 0) nil)
     (t (nth (floor (/ len 2)) list)))))
    
(defun search-mid (sub str)
  (mid (search-all sub str)))
From: Pascal Bourguignon
Subject: Re: What is wrong
Date: 
Message-ID: <87mzl560wz.fsf@thalassa.informatimago.com>
··················@gmail.com writes:

> I'm a lisp newbie too. I'm not sure if I understood the second problem
> well, but here is my solution for it.
>
> (defstruct node label left right)
>
> (defun split-leaves (node)
>   (let* ((the-label (node-label node))
> 	 (index (search-mid " " the-label)))
>     (cond
>      ((null index) node)
>      (t
>       (let ((left-str (subseq the-label 0 index))
> 	    (right-str (subseq the-label (1+ index))))
> 	  (make-node :label the-label
> 		     :left (split-leaves (make-node :label left-str))
> 		     :right (split-leaves (make-node :label right-str))))))))
>
> (defun search-all (sub str &optional (start 0))
>   (let ((found (search sub str :start2 start)))
>     (cond
>      ((null found) nil)
>      (t (cons found (search-all sub str (+ found (length sub))))))))
>
> (defun mid (list)
>   (let ((len (length list)))
>     (cond
>      ((= len 0) nil)
>      (t (nth (floor (/ len 2)) list)))))
>     
> (defun search-mid (sub str)
>   (mid (search-all sub str)))


This is not exactly what I meant.  But your program nicely split the
words to the leaves.  (However, it gives leaves with empty strings when
there are several spaces in sequence).



The recursion was needed only to find the leaves, but the processing
on the leaves should be done once.

(defun leafp (node) (and (null (node-left node)) (null (node-right node))))

(defun split-leaves (node)
  (if (leafp node)
      (let* ((label (node-label node))
             (mid   (truncate (length label) 2)))
        (make-node :label label
                   :left  (make-node :label (subseq label 0 mid))
                   :right (make-node :label (subseq label mid))))
      (make-node :label (node-label node)
                 :left  (and (node-left  node)
                             (split-leaves (node-left  node)))
                 :right (and (node-right node)
                             (split-leaves (node-right node))))))

[52]> (loop
         :with tree = (make-node :label "L'algorithme en  question  a  ete  publie  en  1960  dans l'IBM Journal,")
         :repeat 2
         :do (setf tree (split-leaves tree)) (print tree) (terpri))

#S(NODE 
   :LABEL "L'algorithme en  question  a  ete  publie  en  1960  dans l'IBM Journal," 
   :LEFT #S(NODE 
            :LABEL "L'algorithme en  question  a  ete  p" 
            :LEFT NIL 
            :RIGHT NIL) 
   :RIGHT #S(NODE 
             :LABEL "ublie  en  1960  dans l'IBM Journal," 
             :LEFT NIL 
             :RIGHT NIL)) 

#S(NODE 
   :LABEL "L'algorithme en  question  a  ete  publie  en  1960  dans l'IBM Journal," 
   :LEFT #S(NODE 
            :LABEL "L'algorithme en  question  a  ete  p" 
            :LEFT #S(NODE 
                     :LABEL "L'algorithme en  q" 
                     :LEFT NIL 
                     :RIGHT NIL) 
            :RIGHT #S(NODE 
                      :LABEL "uestion  a  ete  p" 
                      :LEFT NIL 
                      :RIGHT NIL)) 
   :RIGHT #S(NODE 
             :LABEL "ublie  en  1960  dans l'IBM Journal," 
             :LEFT #S(NODE 
                      :LABEL "ublie  en  1960  d" 
                      :LEFT NIL 
                      :RIGHT NIL) 
             :RIGHT #S(NODE 
                       :LABEL "ans l'IBM Journal," 
                       :LEFT NIL 
                       :RIGHT NIL))) 



-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

Nobody can fix the economy.  Nobody can be trusted with their finger
on the button.  Nobody's perfect.  VOTE FOR NOBODY.
From: Edi Weitz
Subject: Re: What is wrong
Date: 
Message-ID: <ubr1malpx.fsf@agharta.de>
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")
From: Pascal Bourguignon
Subject: Re: What is wrong
Date: 
Message-ID: <87u0fe7p88.fsf@thalassa.informatimago.com>
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."