A string of letters, numbers, or special characters other than parentheses, similar to a string. Examples
atom 1982 $abc*
A collection of S-expressions, similar to an array Examples
(a b c) (a (x y x) (b) c)
Returns the first S-expression form a list. Arguments a non-empty list Examples
car (a b c) # => a
Ruby Equivalent
['a', 'b', 'c'].first # => 'a'
Returns the passed list with the first S-expression removed. Arguments a non-empty list Example
cdr (a b c) # => (b c)
Ruby Equivalent
['a', 'b', 'c'].unshift # => ['b', 'c']
Adds the first argument (an S-expression) to the beginning of the second argument (a list). Arguments takes two arguments (1) any S-expression and (2) any list. Example
cons z (a b c) # => (z a b c)
Ruby Equivalent
['a', 'b', 'c'].unshift('z') # => ['z', 'a', 'b', 'c']
Checks if the passed argument is an empty list. Returns boolean. In practice, it returns false for everything except an empty list. Arguments a list Example
null? (a b c) # => false
Ruby Equivalent
['a', 'b', 'c'].empty? # => false
Check if the passed argument is an atom. Returns boolean. Arguments any S-expression Example
atom? abc # => true
Ruby Equivalent
'abc'.is_a?(String) # => true
Tests equality of two non-numeric atoms. Returns boolean. Arguments takes two arguments – both of them must be non-numeric atoms Example
eq? abc abc # => true
Ruby Equivalent
'abc' == 'abc' # => true
Checks if the passed list contains only atoms (no lists). Returns boolean. Arguments takes a list Example
lat? (a b c) # => true
Ruby Equivalent
['a', 'b', 'c'].all? { |i| i.is_a?(String) }
(cond . . . ) asks questions
(lambda . . . ) creates a function;
(define . . . ) gives it a name.
(or … ) asks two questions, one at a time. If the first one is true it stops and answers true. Otherwise it asks the second question and answers with whatever the second question answers. Example
(or (null? (a b c)) (atom? dummy) ) # => true
Ruby Equivalent
['a', 'b', 'c'].empty? || 'dummy'.is_a(String) # => true
Write the function lat?
using some, but not necessarily all, of the following functions: car
cdr
cons
null?
atom?
and eq?
LISP
# sudo code lambda lat?(array) return false unless (atom? (car array)) (null? array) ? true : (lat? (cdr array)) end # actual answer (define lat? (lambda (l) (cond ((null? l) #t) ((atom? (car l)) (lat? (cdr l))) (else #f))))
Ruby
def lat?(array) return false unless array.shift.is_a?(String) array.empty? ? true : lat?(array) end