|
| 1 | +# Scheme Fraglet Guide |
| 2 | + |
| 3 | +## Language Version |
| 4 | +Chibi Scheme (R7RS-compliant) |
| 5 | + |
| 6 | +## Execution Model |
| 7 | +- Interpreted, runs directly from source |
| 8 | +- Code executes at the top level in order |
| 9 | +- File-based execution via shebang or interpreter |
| 10 | + |
| 11 | +## Key Characteristics |
| 12 | +- S-expression syntax (parentheses-based) |
| 13 | +- Dynamic typing |
| 14 | +- Case-sensitive |
| 15 | +- First-class functions |
| 16 | +- Lexical scoping |
| 17 | +- Tail-call optimization |
| 18 | + |
| 19 | +## Fragment Authoring |
| 20 | +Write valid Scheme expressions. Your fragment becomes the script body, so code executes in order. Define functions and variables before using them. |
| 21 | + |
| 22 | +## Available Libraries |
| 23 | +- Standard Scheme procedures (R7RS) |
| 24 | +- Chibi Scheme extensions via `(import (chibi))` |
| 25 | +- No additional packages are pre-installed |
| 26 | + |
| 27 | +## Common Patterns |
| 28 | +- Output: `(display "message")` followed by `(newline)` |
| 29 | +- Function definition: `(define (function-name args) body)` |
| 30 | +- Variable binding: `(define variable value)` |
| 31 | +- Let binding: `(let ((var value)) body)` |
| 32 | +- List processing: `(map function list)` |
| 33 | +- Conditionals: `(if condition then else)` |
| 34 | +- Recursion: Tail-recursive functions are optimized |
| 35 | + |
| 36 | +## Examples |
| 37 | +```scheme |
| 38 | +;; Simple output |
| 39 | +(display "Hello, World!") |
| 40 | +(newline) |
| 41 | +
|
| 42 | +;; Function definition |
| 43 | +(define (greet name) |
| 44 | + (display "Hello, ") |
| 45 | + (display name) |
| 46 | + (display "!") |
| 47 | + (newline)) |
| 48 | +
|
| 49 | +(greet "Alice") |
| 50 | +
|
| 51 | +;; List processing |
| 52 | +(define numbers '(1 2 3 4 5)) |
| 53 | +(define squared (map (lambda (x) (* x x)) numbers)) |
| 54 | +(display "Sum of squares: ") |
| 55 | +(display (apply + squared)) |
| 56 | +(newline) |
| 57 | +
|
| 58 | +;; Recursive function |
| 59 | +(define (factorial n) |
| 60 | + (if (<= n 1) |
| 61 | + 1 |
| 62 | + (* n (factorial (- n 1))))) |
| 63 | +
|
| 64 | +(display "Factorial of 5: ") |
| 65 | +(display (factorial 5)) |
| 66 | +(newline) |
| 67 | +``` |
| 68 | + |
| 69 | +## Caveats |
| 70 | +- Fragments should be idempotent—design them so repeated runs succeed without manual cleanup |
| 71 | +- Each run starts fresh—include all setup logic in the fragment itself |
| 72 | +- Use `(display)` for output, followed by `(newline)` for line breaks |
| 73 | +- Chibi Scheme supports R7RS standard procedures |
0 commit comments