shuki shtein
shuki shtein

Reputation: 75

Could not undersand racket function

My proffesor gave us this function:

(: every? : (All (A) (A -> Boolean) (Listof A) -> Boolean))

(define (every? pred lst)
  (or (null? lst)
      (and (pred (first lst))
           (every? pred (rest lst)))))

I couldn't understand the meaning of: All (A) (A -> Boolean). please can someone can explain to me - what is the meaning of the variable, what the function get, what is it do and what is it return because i can't figure it out.

Upvotes: 2

Views: 106

Answers (1)

soegaard
soegaard

Reputation: 31147

Let's give the function every? a spin at the repl:

> (every? even? (list 1 2 3 4))
#f

> (every? char? (list #\a #\b #\c))
#t

Note that the type of the first list (list 1 2 3 4) is (Listof Number). The type of the second list (list #\a #\b #\c) is (Listof Char).

What type should the lst argument of every? have? Clearly it needs to be a list, but what type are the elements? We don't know, so we make it a (Listof A), where A stands for some (unknown) type.

However the predicate pred is called on the elements in the list, so the type must match. In the first example: even? has the type "function from number to boolean" aka (A -> Boolean).

In general we need the type: (A -> Boolean) for the predicate.

This becomes:

  (: every? : (All (A) (A -> Boolean) (Listof A) -> Boolean))

Upvotes: 5

Related Questions