Giving nil a value

Q&A's, tips, howto's

Giving nil a value

Postby tomtoo » Tue Nov 07, 2017 1:02 am

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.
tomtoo
 
Posts: 46
Joined: Wed Oct 28, 2009 10:00 pm

Re: Giving nil a value

Postby fdb » Tue Nov 07, 2017 12:42 pm

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)
fdb
 
Posts: 34
Joined: Sat Nov 09, 2013 8:49 pm


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 2 guests

cron