listp

Discussion of Common Lisp

listp

Postby GengyangCai » Sat Jun 03, 2017 9:44 am

Can anyone explain how does listp in Lisp work ?

CL-USER 2 : 1 > (if (listp 1) (+ 1 2) (+ 3 4))
7

What's this about ?

So you have an "if" , a "listp 1" , a (+ 1 2) and a (+ 3 4) and the output result is 7. How does this work ?
GengyangCai
 
Posts: 14
Joined: Sun Jul 08, 2012 3:50 pm

Re: listp

Postby pjstirling » Sat Jun 03, 2017 10:35 am

IF is a SPECIAL-OPERATOR, it evaluates its first child for being non-NIL, if that's the case then it evaluates its second child and returns that as its result, or else it evaluates its third child and returns that.

(LISTP 1) evaluates to NIL, because 1 is not a CONS, so it evaluates (+ 3 4)

+ is the function that adds all of its arguments together, (+ 3 4) evaluates to 7
pjstirling
 
Posts: 135
Joined: Sun Nov 28, 2010 4:21 pm


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 2 guests

cron