0

If the predicate bit? returns #t if the items is 1 or 0

(define bit?
  (lambda (item)
    (if (equal? item 0)
        #t
    (if (equal? item 1)
        #t
        #f))))

how can i use bit? in a new procedure, all-bits? that returns #t if all items in the list are bits?
I can't figure it out
this is what i tried.

 (define all-bits?
      (lambda (ls)
        (cond
          [(null? ls) #t]
          [(equal? (car ls all-bits?)) bit?]
          [else (all-bits? ls (cdr ls))]))
Frankie V.
  • 23
  • 3
  • 2
    Looks like it. I'd be willing to bet that the helper function was given as part of the question, because it is syntactically correct, unlike almost every part of the main function (sorry, Frankie, but that's how it is). – itsbruce Oct 09 '12 at 13:53

1 Answers1

1

The 2nd of your cond conditions is entirely wrong. It is syntactically wrong; equal? takes two parameters, car only takes one and bit also requires one, yet you are passing 1, 2 and 0 parameters, respectively. It also makes no sense: your helper function bit? is designed to test a single item, yet you're not testing anything with it. Why is all-bits? in that line at all?

The 3rd cond line is syntactically wrong: again, you're passing two parameters to a function that only takes one.

You need to be saying 'Return true if the first atom is a bit and the second atom is a bit and (and so on, and so on). That structure simply isn't represented in your code.

I'd also argue that all-bits should return false for the empty list and true where you have a list with a single bit, but maybe (all-bits? '()) ==> #t was specified in the original course question.

To do it your way (true for the empty list), your cond should only have two statements;

(cond
   ((null? l) #t)
   (else ("Return true if the first atom is a bit **and** if the second atom is a bit **and** the third (and so on, and so on, recursively)"))

To do it my way, it would look like this:

(cond
   ((null? l) #f)
   ((and (null? (cdr l)) ("something here to test that (car l) is a bit")) #t)
   (else ("Return true if the first atom is a bit **and** if the second atom is a bit **and** the third (and so on, and so on, recursively)")).

Should be clear that the bits in quotes are not real code.

I haven't done your homework for you, but I hope all my comments have made it more clear what you have to do. Even if you try to do your version rather than mine, mine actually includes a big hint to how you should structure the final line.

If you haven't yet been shown how to use and, use nested if statements.

itsbruce
  • 4,825
  • 26
  • 35