Heplar
Heplar

Reputation: 65

Currying using Scheme

I have three functions for a practice exam that I am struggling on.

A function that takes a predicate "pred" and a set "x" and returns whether or not the predicate is true for all elements in the set.

What I was trying:

(define (all? pred x)
  (lambda (t) 
    (equal? (pred t) x)))

Since pred t returns the subset of x where the predicate is true, I was trying to compare it to the original set... Which obviously isn't the way to do it.

A function that takes an operation "op" and a set "x" and returns a new set where basically the op function has been mapped to the entire set. Basically the equivalent of map, so you'd think I shouldn't be asking for help on this...

What I am trying:

(define (map op x)
  (lambda (t) 
    (map (op t))))

I must be missing some basic aspect of currying because I feel like these operations should be simple..

Upvotes: 0

Views: 1109

Answers (1)

David Merinos
David Merinos

Reputation: 1295

So you're trying to do something like andmap

You could define a function that evaluates a list to see wether all their elements are #t values.

(define full-true?
  (λ (lst)
   (if (empty? lst) #t
     (if (car lst) (full-true? (cdr lst))
         (car lst)))
))

Then, your main function would be:

(define for-all?
 (lambda 
   (pred lst-of-items)
    (full-true? (map (lambda (x) (pred x)) lst-of-items))
))

Upvotes: 1

Related Questions