Reputation: 789
I'm just learning how to write in Scheme and hitting a road block with the use of Null. I have the script below from this video series...
https://www.youtube.com/watch?v=Qqext1NwmqM&index=2&list=PLgyU3jNA6VjRMB-LXXR9ZWcU3-GCzJPm0
Script...
(define (my-map fn lst)
(if (null? lst)
null
(cons (fn (car lst)) (my-map fn (cdr lst)))))
This is just redefining the map function in Scheme. It fails at the third line as "null: undefined. cannot reference undefined identifier" when I pass a function and a list.
I just copied what the video shows so not sure why it's failing. If I switch out null for '(), that works. Anyone know why?
Upvotes: 0
Views: 43
Reputation: 236124
It's quite possible that your Scheme interpreter doesn't have the null
symbol bound to the '()
value, as this is not required by the language specification. Either do this:
(define null '())
Or equivalently this, as you already guessed:
(define (my-map fn lst)
(if (null? lst)
'()
(cons (fn (car lst))
(my-map fn (cdr lst)))))
Notice that the latest Scheme report states that (null? obj)
should return #t
if
obj
is the empty list, otherwise returns#f
So it's ok to define null
as '()
.
Upvotes: 1