define introduces top-level values and functions.
Syntax
⟨monomorphize-directive⟩?
⟨inline-directive⟩?
⟨declare-directive⟩?
(define ⟨def⟩ ⟨docstring⟩? ⟨expr⟩)
;; ⟨def⟩ := ⟨name⟩
;; | *⟨name⟩*
;; | (⟨name⟩ ⟨arg⟩...)
;;
;; ⟨arg⟩ := ⟨name⟩
;; | _
;; | ⟨pattern⟩
Semantics
- Defines a new value or function.
- Type is automatically inferred, but a
declareis recommended. - Can be inlined, monomorphized, or specialized.
- Parameters which are not referenced can be named
_or_xwherexcan be any name. - If an argument is a data type with one constructor, a direct pattern match can be used.
- The body of a function definition can
returnexplicitly. - When
⟨name⟩begins and ends with*, as in*base*, thendefinedefines a dynamic variable. Otherwise, it defines an ordinary lexical variable.
Example
(define (add2 x)
(+ x 2))
;; dynamic variable
(define *base* 10)