File: cond

package info (click to toggle)
scheme9 2025.08.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,080 kB
  • sloc: lisp: 16,752; ansic: 11,869; sh: 806; makefile: 237; sed: 6
file content (45 lines) | stat: -rw-r--r-- 1,648 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
R4RS 4.2.1  (cond <clause1> <clause2> ...)  ==>  object

Syntax: Each <clause> should be of the form

(<test> <expression> ...)

where <test> is any expression. The last <clause> may be an "else
clause," which has the form

(else <expression1> <expression2> ...).

Semantics: A COND expression is evaluated by evaluating the <test>
expressions of successive <clause>s in order until one of them
evaluates to a true value (see section see section 6.1 Booleans).
When a <test> evaluates to a true value, then the remaining
<expression>s in its <clause> are evaluated in order, and the result
of the last <expression> in the <clause> is returned as the result
of the entire COND expression. If the selected <clause> contains
only the <test> and no <expression>s, then the value of the <test>
is returned as the result. If all <test>s evaluate to false values,
and there is no ELSE clause, then the result of the conditional
expression is unspecified; if there is an ELSE clause, then its
<expression>s are evaluated, and the value of the last one is
returned.

(cond ((> 3 2) 'greater)
      ((< 3 2) 'less))    ==>  greater

(cond ((> 3 3) 'greater)
      ((< 3 3) 'less)
      (else 'equal))      ==>  equal

Some implementations support an alternative <clause> syntax,

(<test> => <recipient>),

where <recipient> is an expression. If <test> evaluates to a true
value, then <recipient> is evaluated. Its value must be a procedure
of one argument; this procedure is then invoked on the value of the
<test>.

(cond ((assv 'b '((a 1) (b 2))) => cadr)
      (else #f))                          ==>  2

S9fES does support the alternative clause syntax.