What is the meaning of '?' in the Racket docs?

Nil*_*ils 2 racket

Example:

(number? v) ? boolean?

  v : any/c
Run Code Online (Sandbox Code Playgroud)

I understand the '?' behind 'number' but the second '?', behind 'boolean' irritates me. Does it mean that it maybe returns a boolean, and maybe not?

Edit for clarity

I come from Python, to me this reads as: is_number::function returns is_boolean::function and not is_number::function returns bool::bool.

Solution

As Jérôme explained, it reads as:

is_number(v) returns b where is_boolean(b) == True
Run Code Online (Sandbox Code Playgroud)

Jér*_*tin 5

boolean? is a predicate. It's a function that has (by convention) a question mark at the end to show it can be used to check for the type of a value and return a boolean, like so:

> (boolean? #f)
#t
> (boolean? "hello")
#f
Run Code Online (Sandbox Code Playgroud)

By default Racket is dynamically typed, so in order to show type information in the documentation, predicates are used as a kind of type annotation.

So basically, (number? v) ? boolean? means "The function number? returns a value which, when passed to the predicate boolean?, returns true".

It becomes useful when you have more complex predicates:

(pick-random-stuff bag?) ? (listof (or/c toy? food? paperclip? aligator?))
Run Code Online (Sandbox Code Playgroud)

This function returns a list containing any amount of those different objects in any order.

Notes

It is considered a good practice to always name your predicates with a ? at the end.

You might have noticed though that in my examples, listof and or/c don't have question marks. It's because they are not predicates themselves, but functions that build predicates.

In most lisp languages, like Scheme, Racket, Clojure, or Common Lisp, a lot of symbols that have specific meanings in other languages are just valid identifiers. ?, =, -, ->, + are all allowed inside variable and function names.

  • 该函数实际上不会被调用。它用作类型注释。这意味着:“这个函数必须返回一个值,当传递给 `boolean?` 时,返回 true。” (2认同)
  • 它很有用,因为它允许链接到相应谓词的文档。当你点击 `boolean?` 时,你会被重定向到 `boolean?` 的描述。在定义很多自定义类型时,比如`person?`、`dinosaur?`、`home?`,使用谓词更容易。你也可以有更复杂的谓词:`(listof (or/c number?boolean?))`。 (2认同)