C
Carsten Eckelmann
Hi everybody,
I have spent the night reading about the wonders of Scheme at
http://www.teach-scheme.org and have stumbled over a small demo program.
This function checks whether a guest is on the guest list or not:
(define (guest name list)
(cond
((empty? list) 'no)
((equal? name (first list)) 'yes)
(else (guest name (rest list)) )))
I am in love with Ruby and I'm not about to switch to Scheme, so I
wanted to write this in ruby:
def guest (name, list)
if list.empty?
:no
elseif name == list.first
:yes
else
guest(name, list.rest)
end
end
Well looks nice but there's a little something missing: there's no such
method "rest" for an array! Whereas there are things like Array#first
and Array#last, this seems to have been forgotten. Of course I could
write it like this:
def guest(name list)
if list.empty?
:no
elsif name == list.shift
:yes
else
guest(name,list)
end
end
But this would change my list in place, which is definetely not what I
want. And I could also just add the rest function to Array:
class Array
def rest()
self[1..self.length]
end
end
Easy enough, but still, why isn't this included per default in the array
class? Or am I the only one missing it?
Cheers,
Carsten.
I have spent the night reading about the wonders of Scheme at
http://www.teach-scheme.org and have stumbled over a small demo program.
This function checks whether a guest is on the guest list or not:
(define (guest name list)
(cond
((empty? list) 'no)
((equal? name (first list)) 'yes)
(else (guest name (rest list)) )))
I am in love with Ruby and I'm not about to switch to Scheme, so I
wanted to write this in ruby:
def guest (name, list)
if list.empty?
:no
elseif name == list.first
:yes
else
guest(name, list.rest)
end
end
Well looks nice but there's a little something missing: there's no such
method "rest" for an array! Whereas there are things like Array#first
and Array#last, this seems to have been forgotten. Of course I could
write it like this:
def guest(name list)
if list.empty?
:no
elsif name == list.shift
:yes
else
guest(name,list)
end
end
But this would change my list in place, which is definetely not what I
want. And I could also just add the rest function to Array:
class Array
def rest()
self[1..self.length]
end
end
Easy enough, but still, why isn't this included per default in the array
class? Or am I the only one missing it?
Cheers,
Carsten.