Giving nil a value

Started by tomtoo, November 06, 2017, 05:02:24 PM

Previous topic - Next topic

tomtoo

Hi guys,



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



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


I can do

(setf (a 2) "none")


but

(dolist (i a)(when (null? i)(setf i "none")))


returns nil. and
(null? (a 2))
returns true.



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

thank you.

fdb

#1
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.

(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:

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

(replace nil a "none")

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