hi all, trying to write a function nonlat? which returns true if the given list does not contain any atoms. so an empty list would be a nonlat and return true.

the tests i'm supposed to use are:

(nonlat? '(a b c))
(nonlat? '(a (b) (c)))
(nonlat? '((a b) ((c)) (d)))
(nonlat? '())

and the results are supposed to be #f, #f, #t and #t

i've managed to get this code but not sure how to get the third to turn out a #t instead of a #f. i've only gotten it down to this, pretty basic but a start

(define nonlat?
(lambda (list)
(null? list)))

second problem is to write a function member-cat? which returns true if the atom cat is a member of the given list, otherwise it returns false. it's also supposed to only take 1 argument which is the reason for the tests being like they are

the tests for this function is:
(member-cat? '())
(member-cat? '(bird cat dog))
(member-cat? '(hamster turtle))
(member-cat? '(cat))

and it should return #f, #t, #f, #t respectively. so far with the second function i've managed to get it to work for all but the second test, classmate says to use recursive call and hard code 'cat into it but not sure exactly how to do that

(define member-cat?
(lambda (lat)
(cond
((null? lat) #f)
(else (or (eq? 'cat (car lat)))))))

3
Contributors
2
Replies
5
Views
8 Years
Discussion Span
Last Post by Harleqin

i've managed to get this code but not sure how to get the third to turn out a #t instead of a #f. i've only gotten it down to this, pretty basic but a start

(define nonlat?
(lambda (list)
(null? list)))

Not much of a start.

``````(define nonlat?
(lambda (list)
(null? list)))``````

So, this "nonlat?" is a function that takes one parameter, which is named "list" internally. It returns the result of calling "null?" on that parameter, i.e. the list. Of course, to do that, you can as well just call "null?" instead of "nonlat?". What you really want to do is apply "null?" to each member of the list. There are several approaches to this, but I guess that the most basic one is to first check the first element, and if it is OK, recurse on the rest. You most likely recognize what I am saying from what you have been taught in your course.

``````(define member-cat?
(lambda (lat)
(cond
((null? lat) #f)
(else (or (eq? 'cat (car lat)))))))``````

Here, you are missing an important case: what do you want to do if lat is not null, and (car lat) is not equal to 'cat? Note that this is better written as terms of the COND:

``````(define member-cat?
(lambda (lat)
(cond
((null? lat) #f)
((eq? 'cat (car lat)) #t)
(else ; ... what comes here?
))))``````

Edited by Harleqin: code formatting, add fix

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.