AtlatestRepositorysigil-web-styles
sigil-web-styles / tree / build / dev / lib / _pkg / sigil-stdlib / languagespecial-forms.md
2
# Special Forms4
> Core syntax constructs for definitions and control flow.6
## Definitions8
### define10
Define a variable or procedure.12
```scheme13
;; Variable definition14
(define pi 3.14159)15
(define message "hello")17
;; Procedure definition (shorthand)18
(define (square x)19
(* x x))21
;; Equivalent to:22
(define square23
(lambda (x) (* x x)))25
;; Multiple expressions in body26
(define (greet name)27
(display "Hello, ")28
(display name)29
(newline))30
```32
### lambda34
Create an anonymous procedure.36
```scheme37
(lambda (x) (* x x)) ; one parameter38
(lambda (x y) (+ x y)) ; two parameters39
(lambda args (length args)) ; rest parameter (collects all args)40
(lambda (x . rest) x) ; one required, rest optional41
```43
### set!45
Mutate an existing binding.47
```scheme48
(define counter 0)49
(set! counter (+ counter 1))50
```52
Use sparingly. Prefer functional style when possible.54
## Local Bindings56
### let58
Bind variables in parallel (bindings can't reference each other).60
```scheme61
(let ((x 1)62
(y 2))63
(+ x y))64
; => 366
;; Named let for recursion67
(let loop ((n 5) (acc 1))68
(if (zero? n)69
acc70
(loop (- n 1) (* acc n))))71
; => 120 (factorial)72
```74
### let*76
Bind variables sequentially (each can see previous bindings).78
```scheme79
(let* ((x 1)80
(y (+ x 1))81
(z (+ y 1)))82
(list x y z))83
; => (1 2 3)84
```86
### letrec88
Bind variables that can reference each other (for mutual recursion).90
```scheme91
(letrec ((even? (lambda (n)92
(or (zero? n) (odd? (- n 1)))))93
(odd? (lambda (n)94
(and (not (zero? n)) (even? (- n 1))))))95
(even? 10))96
; => #t97
```99
## Conditionals101
### if103
Two-branch conditional.105
```scheme106
(if (> x 0)107
"positive"108
"not positive")110
;; Without else branch (returns unspecified value if false)111
(if (file-exists? path)112
(load-file path))113
```115
### cond117
Multi-branch conditional with pattern-like clauses.119
```scheme120
(cond121
((< x 0) "negative")122
((= x 0) "zero")123
(else "positive"))125
;; With => to pass test result to procedure126
(cond127
((assoc key alist) => cdr)128
(else default))129
```131
### case133
Dispatch on value using `eq?` comparison. Best for symbols and small integers.135
```scheme136
(case color137
((red green blue) "primary")138
((cyan magenta yellow) "secondary")139
(else "other"))140
```142
### when / unless144
Single-branch conditionals for side effects.146
```scheme147
(when (> x 0)148
(display "positive")149
(newline))151
(unless (null? items)152
(process-items items))153
```155
## Boolean Operations157
### and159
Short-circuit AND. Returns last true value or first false.161
```scheme162
(and #t #t) ; => #t163
(and 1 2 3) ; => 3164
(and #f (error)) ; => #f (error not evaluated)165
```167
### or169
Short-circuit OR. Returns first true value or last false.171
```scheme172
(or #f #f) ; => #f173
(or #f 1 2) ; => 1174
(or (find x) default) ; common pattern175
```177
### not179
Boolean negation.181
```scheme182
(not #f) ; => #t183
(not #t) ; => #f184
(not '()) ; => #f (only #f is false)185
```187
## Sequencing189
### begin191
Execute expressions in order, return last value.193
```scheme194
(begin195
(display "one")196
(display "two")197
"result")198
; prints: onetwo199
; => "result"200
```202
## Iteration204
For iteration, use named `let` or higher-order functions like `map` and `fold`.206
### Named let208
```scheme209
;; Count down210
(let loop ((n 5))211
(when (> n 0)212
(display n)213
(loop (- n 1))))214
; prints: 54321216
;; Accumulating loop217
(let loop ((items '(1 2 3 4 5))218
(sum 0))219
(if (null? items)220
sum221
(loop (cdr items) (+ sum (car items)))))222
; => 15223
```225
### Higher-order functions227
```scheme228
;; Transform each element229
(map (lambda (x) (* x 2)) '(1 2 3)) ; => (2 4 6)231
;; Keep matching elements232
(filter even? '(1 2 3 4 5)) ; => (2 4)234
;; Reduce to single value235
(fold-left + 0 '(1 2 3 4 5)) ; => 15236
```