Page 1 of 1

Giving nil a value

Posted: Tue Nov 07, 2017 1:02 am
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.

Re: Giving nil a value

Posted: Tue Nov 07, 2017 12:42 pm
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)