Izzy
Izzy

Reputation: 29

Lisp : Function prints NIL for list

I problem I'm running into is that when I make a function that prints a certain part of a list it prints it as NIL and not the actual element(s).

Ex:

> (setf thelist '((a b) (c (d e f)) (g (h i)))
> (defun f1(list)
        (print ( car (list))))
> (f1 thelist)
   NIL
   NIL

But this works: 
> (car thelist)
   (A B)
  1. Why does it print NIL when thelist is used in the function but when used alone it outputs the first element just fine?
  2. How do I get the function to print the part of the list I want out?

Upvotes: 0

Views: 334

Answers (1)

C. K. Young
C. K. Young

Reputation: 223023

You have:

(print (car (list)))

This is invoking the list function, and not using your list parameter. (list) always returns an empty list. (Common Lisp is a "Lisp-2", which means that list in a function-call context refers to a different thing from list in a variable-access context.)

To fix, change your code to use:

(print (car list))

instead.

Upvotes: 11

Related Questions