1
votes

I have been trying to understand this exercise 8.10 in "simply scheme", but just cannot grasp it in Dr Racket..

It's this one: Write a predicate true-for-all? that takes two arguments, a predicate procedure and a sentence. It should return #t if the predicate argument returns true for every word in the sentence.

(true-for-all? even? ’(2 4 6 8))

T

(true-for-all? even? ’(2 6 3 4))

F

I tried the solution:

(define (true-for-all? pred sent) (= (count sent) (count (keep pred sent))))

but Dr Racket gives errors like count: arity mismatch. Is anyone able to rewrite it or at least give some hints. Appreciated a lot:)

Best Regards, Eunice

2

2 Answers

0
votes

Racket supports many languages. Therefore all programs begins with a #lang line that tells Racket which language to use. The dialect of Scheme used in Simply Scheme needs this line:

#lang planet dyoo/simply-scheme:2

The following program:

#lang planet dyoo/simply-scheme:2

(define (true-for-all? pred sent)
  (= (count sent) (count (keep pred sent))))

(true-for-all? even? '(2 4 6 8))

(true-for-all? even? '(2 6 3 4))

returns

#t
#f

as expected.

Note: In the lower left corner of DrRacket choose "Determine language from source".

The documentation for the Simply Scheme languages is here: http://planet.racket-lang.org/package-source/dyoo/simply-scheme.plt/2/2/planet-docs/manual/index.html

0
votes

There's a solution in pure Racket (just for context):

#lang racket
(define  (true-for-all? pred list)
  (cond
    [(empty? list) #t]
    [(pred (first list)) (true-for-all? pred (rest list))]
    [else #f]))