Defining FunctionsΒΆ

Function definitions have the form:

(define <function name>
<rules>)

or

(define <function name>
<signature>
<rules>)

(See Types (functions) for more on this).

A function name is a symbol beginning in lowercase. Rules have the form

<arguments> -> <result>

or

<arguments> -> <result> where <guard>

<- is used in place of -> if backtracking is needed (see fast reference; backtracking).

Arguments can be any atom (string, symbol, number, boolean) or lists or vectors of such (see Functions: pattern matching). Variables are symbols beginning in uppercase. _ is a wildcard.

(0-) (define likes
       tom dick -> yes
       dick harry -> yes
       harry tom -> yes)
likes

(1-) (likes tom dick)
yes

(2-) (likes dick fred)
partial function likes
track likes? (y/n) n

(3-) (define greater-or-equal
       X Y -> X where (> X Y)
       _ Y -> Y)
greater-or-equal

(4-) (greater-or-equal 4 3)
true

(5-) (define prefix?
       [] _ -> true
       [X | Y] [X | Z] -> (prefix? Y Z)
       _ _ -> false)
prefix?

(6-) (prefix? [1 2 3] [1 2 3 4 5 6])
true

(7-) (prefix? [] 8)
true

(8-) (tc +) \\ switch on type checking
true

(9+) (define prefix?
       {(list A) --> (list A) --> boolean}
       [] _ -> true
       [X | Y] [X | Z] -> (prefix? Y Z)
       _ _ -> false)       
prefix? : ((list A) --> ((list A) --> boolean))

(10+) (prefix? [1 2 3] [1 2 3 4 5 6])
true : boolean

(11+) (prefix? [] 8)
type error