Plaster
New
List
Login
text
default
anonymous
2023.02.26 09:12:30
(defun len (lst) (if (null lst) 0 (+ (len (cdr lst)) 1))) ;<< len returns (cdr lst) where 1 comes from to be added to 1 and where the result of + is being saved between iterations? (len '(a b c)) CL-USER 6 > (trace len) (LEN) ;; It would be good of you if you would explane how to read trace's output ;; or may be there is a more verbose output CL-USER 7 > (len '(a b c)) 0 LEN > ... >> LST : (A B C) 1 LEN > ... >> LST : (B C) 2 LEN > ... >> LST : (C) 3 LEN > ... >> LST : NIL 3 LEN < ... << VALUE-0 : 0 2 LEN < ... << VALUE-0 : 1 1 LEN < ... << VALUE-0 : 2 0 LEN < ... << VALUE-0 : 3 3
Raw
Annotate
Repaste
Edit
Annotations
text
default
anonymous
2023.02.26 17:01:35
The result isn't being saved. The result of the recursive call to len is being computed and when it is returned the + is applied to the result and to 1. Trace initially prints the arguments, and when the function returns prints the return value. You will see that len calls itself repeatedly until the *basis case* which returns and then + is applied, and the caller returns and so on, so the results filter back up. That's why the printed lines move to the right (as len calls itself) and then back out to the left (as the calls return, one by one.)
Raw
Repaste
Edit