Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to get rid of "nil" in the output of this function

5 views
Skip to first unread message

Hyunchul Kim

unread,
Feb 17, 1999, 3:00:00 AM2/17/99
to
Anyone? Thanks.


(defun calc (first)

(let ( (op (read)))

(when (not (eq op '=))

(let ( (next (read)) )

(setf first (funcall op first next))
(calc first)

)
)

(when (eq op '=) (print first) t)

)
)

(calc 0)


Barry Margolin

unread,
Feb 17, 1999, 3:00:00 AM2/17/99
to
In article <Pine.SOL.3.96L.99021...@unix4.andrew.cmu.edu>,
Hyunchul Kim <hyun...@andrew.cmu.edu> wrote:
>Anyone? Thanks.

Reformatted so that it's readable and doesn't take up an entire screen of
my response....

>(defun calc (first)
> (let ((op (read)))


> (when (not (eq op '=))
> (let ((next (read)))
> (setf first (funcall op first next))
> (calc first)))
> (when (eq op '=)
> (print first)
> t)))

If the result is printed by the a recursive call to CALC, the T that it
returns ends up being thrown away. This is because the outer call then
performs the (when (eq op '=) ...) form. In that outer call, OP isn't EQ
to =, so the T is never returned.

If you use IF rather than a pair of WHENs the right thing will happen:

(defun calc (first)
(let ((op (read)))
(if (not (eq op '=))


(let ((next (read)))
(setf first (funcall op first next))
(calc first))

(progn
(print first)
t))))

--
Barry Margolin, bar...@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.

Jeff Sandys

unread,
Feb 17, 1999, 3:00:00 AM2/17/99
to
nil is the evaluation of (calc 0)
because let returns the last item
and the last item is (when (eq op ...
and op as your first read input.
Use trace to see calc work through the frames.

You may want to simplify the function like this,

(defun calc2 (input1)
(let ((op (read)))
(if (eq op '=)
(print input1)
(calc2 (funcall op input1 (read))))))

As a point of style, don't use lisp function names
as your input parameters (unless your using lisp on
an extremely small machine)

Thanks,
Jeff Sandys

Hyunchul Kim wrote:
>
> Anyone? Thanks.
>
> (defun calc (first)

> (let ( (op (read)))


> (when (not (eq op '=))
> (let ( (next (read)) )
> (setf first (funcall op first next))
> (calc first)
> )
> )

0 new messages