Giving nil a value

Q&A's, tips, howto's
Post Reply
tomtoo
Posts: 46
Joined: Wed Oct 28, 2009 10:00 pm

Giving nil a value

Post by tomtoo »

Hi guys,

I would like to replace any and all nils in a list with "none" . If I can see the nil

Code: Select all

(set 'a '(1 2 nil 4))
I can do

Code: Select all

(setf (a 2) "none")
but

Code: Select all

(dolist (i a)(when (null? i)(setf i "none")))
returns nil. and

Code: Select all

(null? (a 2))
returns true.

How might I fix this? I don't know where a nil might pop up.
thank you.

fdb
Posts: 57
Joined: Sat Nov 09, 2013 8:49 pm

Re: Giving nil a value

Post by fdb »

Hi tomtoo,

you'll have to refer to an index in the list to set the correct value in the list, i is just a temporary loop variable, see below.

Code: Select all

(set 'a '(1 2 nil 4))

(dolist (i a)(when (null? i)(setf (a $idx) "none")))
This works but doesn't return the changed list. An easier way is to use the replace function:

Code: Select all

(set 'a '(1 2 nil 4))

(replace nil a "none")

> 
(1 2 nil 4)
(1 2 "none" 4)

Post Reply