9.2 Functions and Types
TBoS p 192 onwards
Shen is an explicitly typed polymorphic language in the manner of Hope; it requires that functions be entered with their types.
A --> B --> C is shorthand for A --> (B --> C). Signatures are entered in {...} immediately after the name of the function.
(1+) (define member
{A --> (list A) --> boolean}
_ [] -> false
X [X | _] -> true
X [_ | Y] -> (member X Y))
(fn member) : (A --> ((list A) --> boolean))
(2+) (define square
{number --> number}
X -> (* X X))
(fn square) : (number --> number)
(3+) (define swap
{(A * B) --> (B * A)}
(@p X Y) -> (@p Y X))
(fn swap) : ((A * B) --> (B * A))
(4+) (define unit-vector?
{(vector A) --> boolean}
(@v _ <>) -> true
_ -> false)
(fn unit-vector?) : ((vector A) --> boolean)
(5+) (define unit-string?
{string --> boolean}
(@s _ "") -> true
_ -> false)
(fn unit-string?) : (string --> boolean)
(6+) (member 1 [1 2 3])
true : boolean
(7+) (square 4)
16 : number
(8+) (swap (@p 1 2))
(@p 2 1) : (number * number)
(9+) (unit-vector? (@v 1 <>))
true : boolean
(10+) (unit-string? "a")
true : boolean
|
|