| 1 | --- |
| 2 | jupyter: |
| 3 | kernelspec: |
| 4 | display_name: Calysto Scheme (Python) |
| 5 | language: scheme |
| 6 | name: calysto_scheme |
| 7 | --- |
| 8 | |
| 9 | <img src="images/logo-64x64.png"/> |
| 10 | <h1>Reference Guide for Calysto Scheme</h1> |
| 11 | |
| 12 | [Calysto Scheme](https://github.com/Calysto/calysto_scheme) is a real Scheme programming language, with full support for continuations, including call/cc. It can also use all Python libraries. Also has some extensions that make it more useful (stepper-debugger, choose/fail, stack traces), or make it better integrated with Python. |
| 13 | |
| 14 | In Jupyter notebooks, because Calysto Scheme uses [MetaKernel](https://github.com/Calysto/metakernel/blob/master/README.rst), it has a fully-supported set of "magics"---meta-commands for additional functionality. This includes running Scheme in parallel. See all of the [MetaKernel Magics](https://github.com/Calysto/metakernel/blob/master/metakernel/magics/README.md). |
| 15 | |
| 16 | Calysto Scheme is written in Scheme, and then translated into Python (and other backends). The entire functionality lies in a single Python file: https://github.com/Calysto/calysto_scheme/blob/master/calysto_scheme/scheme.py However, you can easily install it (see below). |
| 17 | |
| 18 | Please see [Calysto Scheme Language](Calysto%20Scheme%20Language.ipynb) for more details on the Calysto Scheme language. |
| 19 | |
| 20 | ## Installation |
| 21 | |
| 22 | You can install Calysto Scheme with Python3: |
| 23 | |
| 24 | ``` |
| 25 | pip3 install --upgrade calysto-scheme --user -U |
| 26 | python3 -m calysto_kernel install --user |
| 27 | ``` |
| 28 | |
| 29 | or in the system kernel folder with: |
| 30 | |
| 31 | ``` |
| 32 | sudo pip3 install --upgrade calysto-scheme -U |
| 33 | sudo python3 -m calysto_kernel install |
| 34 | ``` |
| 35 | |
| 36 | Change pip3/python3 to use a different pip or Python. The version of Python used will determine how Calysto Scheme is run. |
| 37 | |
| 38 | Use it in the console, qtconsole, or notebook with IPython 3: |
| 39 | |
| 40 | ``` |
| 41 | ipython console --kernel calysto_scheme |
| 42 | ipython qtconsole --kernel calysto_scheme |
| 43 | ipython notebook --kernel calysto_scheme |
| 44 | ``` |
| 45 | |
| 46 | In addition to all of the following items, Calysto Scheme also has access to all of Python's builtin functions, and all of Python's libraries. For example, you can use `(complex 3 2)` to create a complex number by calling Python's complex function. |
| 47 | |
| 48 | ## Jupyter Enhancements |
| 49 | |
| 50 | When you run Calysto Scheme in Jupyter (console, notebook, qtconsole, etc.) you get: |
| 51 | |
| 52 | * TAB completions of Scheme functions and variable names |
| 53 | * display of rich media |
| 54 | * stepper/debugger |
| 55 | * magics (% macros) |
| 56 | * shell commands (! command) |
| 57 | * LaTeX equations |
| 58 | * LaTeX-style variables |
| 59 | * Python integration |
| 60 | |
| 61 | |
| 62 | ### LaTeX-style variables |
| 63 | |
| 64 | Calysto Scheme allows you to use LaTeX-style variables in code. For example, if you type: |
| 65 | |
| 66 | ``` |
| 67 | \beta |
| 68 | ``` |
| 69 | |
| 70 | with the cursor right after the 'a' in beta, and then press TAB, it will turn into the unicode character: |
| 71 | |
| 72 | ``` |
| 73 | β |
| 74 | ``` |
| 75 | |
| 76 | There are nearly 1300 different symbols defined (thanks to the Julia language) and documented here: |
| 77 | |
| 78 | http://docs.julialang.org/en/release-0.4/manual/unicode-input/#man-unicode-input |
| 79 | |
| 80 | Calysto Scheme may not implement all of those. Some useful and suggestive ones: |
| 81 | |
| 82 | * \pi - π |
| 83 | * \Pi - Π |
| 84 | * \Sigma - Σ |
| 85 | * \_i - subscript i, such as vectorᵢ |
| 86 | |
| 87 | ```scheme |
| 88 | (define α 67) |
| 89 | ``` |
| 90 | |
| 91 | ```scheme |
| 92 | α |
| 93 | ``` |
| 94 | |
| 95 | ```scheme |
| 96 | (define i 2) |
| 97 | (define vectorᵢ (vector-ref (vector 0 6 3 2) i)) |
| 98 | vectorᵢ |
| 99 | ``` |
| 100 | |
| 101 | ### Rich media |
| 102 | |
| 103 | ```scheme |
| 104 | (import "calysto.display") |
| 105 | ``` |
| 106 | |
| 107 | ```scheme |
| 108 | (calysto.display.HTML "This is <b>bold</b>, <i>italics</i>, <u>underlined</u>.") |
| 109 | ``` |
| 110 | |
| 111 | ```scheme |
| 112 | (import "calysto.graphics") |
| 113 | ``` |
| 114 | |
| 115 | ```scheme |
| 116 | (define canvas (calysto.graphics.Canvas)) |
| 117 | ``` |
| 118 | |
| 119 | ```scheme |
| 120 | (define ball (calysto.graphics.Circle '(150 150) 100)) |
| 121 | ``` |
| 122 | |
| 123 | ```scheme |
| 124 | (ball.draw canvas) |
| 125 | ``` |
| 126 | |
| 127 | ### Shell commands |
| 128 | |
| 129 | ```scheme |
| 130 | ! ls /tmp |
| 131 | ``` |
| 132 | |
| 133 | ### Stepper/Debugger |
| 134 | |
| 135 | Here is what the debugger looks like: |
| 136 | |
| 137 | <img src="images/stepper_debugger.png"> |
| 138 | |
| 139 | It has breakpoints (click in left margin). You must press Stop to exit the debugger. |
| 140 | |
| 141 | <!-- #region --> |
| 142 | ```scheme |
| 143 | %%debug |
| 144 | |
| 145 | (begin |
| 146 | (define x 1) |
| 147 | (set! x 2) |
| 148 | ) |
| 149 | ``` |
| 150 | <!-- #endregion --> |
| 151 | |
| 152 | ### Python Integration |
| 153 | |
| 154 | You can import and use any Python library in Calysto Scheme. |
| 155 | |
| 156 | In addition, if you wish, you can execute expressions and statements in a Python environment: |
| 157 | |
| 158 | ```scheme |
| 159 | (python-eval "1 + 2") |
| 160 | ``` |
| 161 | |
| 162 | ```scheme |
| 163 | (python-exec |
| 164 | " |
| 165 | def mypyfunc(a, b): |
| 166 | return a * b |
| 167 | ") |
| 168 | ``` |
| 169 | |
| 170 | This is a shared environment with Scheme: |
| 171 | |
| 172 | ```scheme |
| 173 | (mypyfunc 4 5) |
| 174 | ``` |
| 175 | |
| 176 | You can use `func` to turn a Scheme procedure into a Python function, and `define!` to put it into the shared environment with Python: |
| 177 | |
| 178 | ```scheme |
| 179 | (define! mypyfunc2 (func (lambda (n) n))) |
| 180 | ``` |
| 181 | |
| 182 | ```scheme |
| 183 | (python-eval "mypyfunc2(34)") |
| 184 | ``` |
| 185 | |
| 186 | # Differences Between Languages |
| 187 | |
| 188 | ## Major differences between Scheme and Python |
| 189 | |
| 190 | 1. In Scheme, double quotes are used for strings and may contain newlines |
| 191 | 1. In Scheme, a single quote is short for (quote ...) and means "literal" |
| 192 | 1. In Scheme, everything is an expression and has a return value |
| 193 | 1. Python does not support macros (e.g., extending syntax) |
| 194 | 1. In Python, "if X" is false if X is None, False, [], (,) or 0. In Scheme, "if X" is only false if X is #f or 0 |
| 195 | 1. Calysto Scheme uses continuations, not the call stack. However, for debugging there is a pseudo-stack when an error is raised. You can turn that off with (use-stack-trace #f) |
| 196 | 1. Scheme procedures are not Python functions, but there are means to use one as the other. |
| 197 | |
| 198 | ## Major Differences Between Calysto Scheme and other Schemes |
| 199 | |
| 200 | 1. define-syntax works slightly differently |
| 201 | 1. In Calysto Scheme, #(...) is short for '#(...) |
| 202 | 1. Calysto Scheme is missing many standard functions (see list at bottom) |
| 203 | 1. Calysto Scheme has a built-in amb operator called `choose` |
| 204 | 1. For debugging there is a pseudo-stack when errors are raised in Calysto Scheme. You can turn that off with (use-stack-trace #f) |
| 205 | |
| 206 | ### Stack Trace |
| 207 | |
| 208 | Calysto Scheme acts as if it has a call stack, for easier debugging. For example: |
| 209 | |
| 210 | ```scheme |
| 211 | (define fact |
| 212 | (lambda (n) |
| 213 | (if (= n 1) |
| 214 | q |
| 215 | (* n (fact (- n 1)))))) |
| 216 | ``` |
| 217 | |
| 218 | ```scheme |
| 219 | (fact 5) |
| 220 | ``` |
| 221 | |
| 222 | <!-- #region --> |
| 223 | To turn off the stack trace on error: |
| 224 | |
| 225 | ```scheme |
| 226 | (use-stack-trace #f) |
| 227 | ``` |
| 228 | That will allow infinite recursive loops without keeping track of the "stack". |
| 229 | <!-- #endregion --> |
| 230 | |
| 231 | # Calysto Scheme Variables |
| 232 | |
| 233 | ## SCHEMEPATH |
| 234 | SCHEMEPATH is a list of search directories used with (load NAME). This is a reference, so you should append to it rather than attempting to redefine it. |
| 235 | |
| 236 | ```scheme |
| 237 | SCHEMEPATH |
| 238 | ``` |
| 239 | |
| 240 | ```scheme |
| 241 | (set-cdr! (cdr SCHEMEPATH) (list "/var/modules")) |
| 242 | ``` |
| 243 | |
| 244 | ```scheme |
| 245 | SCHEMEPATH |
| 246 | ``` |
| 247 | |
| 248 | ## Getting Started |
| 249 | |
| 250 | Note that you can use the word `lambda` or \lambda and then press [TAB] |
| 251 | |
| 252 | ```scheme |
| 253 | (define factorial |
| 254 | (λ (n) |
| 255 | (cond |
| 256 | ((zero? n) 1) |
| 257 | (else (* n (factorial (- n 1))))))) |
| 258 | ``` |
| 259 | |
| 260 | ```scheme |
| 261 | (factorial 5) |
| 262 | ``` |
| 263 | |
| 264 | ## define-syntax |
| 265 | (define-syntax NAME RULES): a method for creating macros |
| 266 | |
| 267 | ```scheme |
| 268 | (define-syntax time |
| 269 | [(time ?exp) (let ((start (current-time))) |
| 270 | ?exp |
| 271 | (- (current-time) start))]) |
| 272 | ``` |
| 273 | |
| 274 | ```scheme |
| 275 | (time (car '(1 2 3 4))) |
| 276 | ``` |
| 277 | |
| 278 | ```scheme |
| 279 | ;;--------------------------------------------------------------------- |
| 280 | ;; collect is like list comprehension in Python |
| 281 | |
| 282 | (define-syntax collect |
| 283 | [(collect ?exp for ?var in ?list) |
| 284 | (filter-map (lambda (?var) ?exp) (lambda (?var) #t) ?list)] |
| 285 | [(collect ?exp for ?var in ?list if ?condition) |
| 286 | (filter-map (lambda (?var) ?exp) (lambda (?var) ?condition) ?list)]) |
| 287 | |
| 288 | (define filter-map |
| 289 | (lambda (f pred? values) |
| 290 | (if (null? values) |
| 291 | '() |
| 292 | (if (pred? (car values)) |
| 293 | (cons (f (car values)) (filter-map f pred? (cdr values))) |
| 294 | (filter-map f pred? (cdr values)))))) |
| 295 | ``` |
| 296 | |
| 297 | ```scheme |
| 298 | (collect (* n n) for n in (range 10)) |
| 299 | ``` |
| 300 | |
| 301 | ```scheme |
| 302 | (collect (* n n) for n in (range 5 20 3)) |
| 303 | ``` |
| 304 | |
| 305 | ```scheme |
| 306 | (collect (* n n) for n in (range 10) if (> n 5)) |
| 307 | ``` |
| 308 | |
| 309 | ```scheme |
| 310 | ;;--------------------------------------------------------------------- |
| 311 | ;; for loops |
| 312 | |
| 313 | (define-syntax for |
| 314 | [(for ?exp times do . ?bodies) |
| 315 | (for-repeat ?exp (lambda () . ?bodies))] |
| 316 | [(for ?var in ?exp do . ?bodies) |
| 317 | (for-iterate1 ?exp (lambda (?var) . ?bodies))] |
| 318 | [(for ?var at (?i) in ?exp do . ?bodies) |
| 319 | (for-iterate2 0 ?exp (lambda (?var ?i) . ?bodies))] |
| 320 | [(for ?var at (?i ?j . ?rest) in ?exp do . ?bodies) |
| 321 | (for ?var at (?i) in ?exp do |
| 322 | (for ?var at (?j . ?rest) in ?var do . ?bodies))]) |
| 323 | |
| 324 | (define for-repeat |
| 325 | (lambda (n f) |
| 326 | (if (< n 1) |
| 327 | 'done |
| 328 | (begin |
| 329 | (f) |
| 330 | (for-repeat (- n 1) f))))) |
| 331 | |
| 332 | (define for-iterate1 |
| 333 | (lambda (values f) |
| 334 | (if (null? values) |
| 335 | 'done |
| 336 | (begin |
| 337 | (f (car values)) |
| 338 | (for-iterate1 (cdr values) f))))) |
| 339 | |
| 340 | (define for-iterate2 |
| 341 | (lambda (i values f) |
| 342 | (if (null? values) |
| 343 | 'done |
| 344 | (begin |
| 345 | (f (car values) i) |
| 346 | (for-iterate2 (+ i 1) (cdr values) f))))) |
| 347 | ``` |
| 348 | |
| 349 | ```scheme |
| 350 | (define matrix2d |
| 351 | '((10 20) |
| 352 | (30 40) |
| 353 | (50 60) |
| 354 | (70 80))) |
| 355 | |
| 356 | (define matrix3d |
| 357 | '(((10 20 30) (40 50 60)) |
| 358 | ((70 80 90) (100 110 120)) |
| 359 | ((130 140 150) (160 170 180)) |
| 360 | ((190 200 210) (220 230 240)))) |
| 361 | ``` |
| 362 | |
| 363 | ```scheme |
| 364 | (begin |
| 365 | (define hello 0) |
| 366 | (for 5 times do (set! hello (+ hello 1))) |
| 367 | hello |
| 368 | ) |
| 369 | ``` |
| 370 | |
| 371 | ```scheme |
| 372 | (for sym in '(a b c d) do (define x 1) (set! x sym) (print x)) |
| 373 | ``` |
| 374 | |
| 375 | ```scheme |
| 376 | (for n in (range 10 20 2) do (print n)) |
| 377 | ``` |
| 378 | |
| 379 | ```scheme |
| 380 | (for n at (i j) in matrix2d do (print (list n 'coords: i j))) |
| 381 | ``` |
| 382 | |
| 383 | ```scheme |
| 384 | (for n at (i j k) in matrix3d do (print (list n 'coords: i j k))) |
| 385 | ``` |
| 386 | |
| 387 | ```scheme |
| 388 | (define-syntax scons |
| 389 | [(scons ?x ?y) (cons ?x (lambda () ?y))]) |
| 390 | |
| 391 | (define scar car) |
| 392 | |
| 393 | (define scdr |
| 394 | (lambda (s) |
| 395 | (let ((result ((cdr s)))) |
| 396 | (set-cdr! s (lambda () result)) |
| 397 | result))) |
| 398 | |
| 399 | (define first |
| 400 | (lambda (n s) |
| 401 | (if (= n 0) |
| 402 | '() |
| 403 | (cons (scar s) (first (- n 1) (scdr s)))))) |
| 404 | |
| 405 | (define nth |
| 406 | (lambda (n s) |
| 407 | (if (= n 0) |
| 408 | (scar s) |
| 409 | (nth (- n 1) (scdr s))))) |
| 410 | |
| 411 | (define smap |
| 412 | (lambda (f s) |
| 413 | (scons (f (scar s)) (smap f (scdr s))))) |
| 414 | |
| 415 | (define ones (scons 1 ones)) |
| 416 | |
| 417 | (define nats (scons 0 (combine nats + ones))) |
| 418 | |
| 419 | (define combine |
| 420 | (lambda (s1 op s2) |
| 421 | (scons (op (scar s1) (scar s2)) (combine (scdr s1) op (scdr s2))))) |
| 422 | |
| 423 | (define fibs (scons 1 (scons 1 (combine fibs + (scdr fibs))))) |
| 424 | |
| 425 | (define facts (scons 1 (combine facts * (scdr nats)))) |
| 426 | |
| 427 | (define ! (lambda (n) (nth n facts))) |
| 428 | ``` |
| 429 | |
| 430 | ```scheme |
| 431 | (! 5) |
| 432 | ``` |
| 433 | |
| 434 | ```scheme |
| 435 | (nth 10 facts) |
| 436 | ``` |
| 437 | |
| 438 | ```scheme |
| 439 | (nth 20 fibs) |
| 440 | ``` |
| 441 | |
| 442 | ```scheme |
| 443 | (first 30 fibs) |
| 444 | ``` |
| 445 | |
| 446 | ## for-each |
| 447 | (for-each PROCEDURE LIST): apply PROCEDURE to each item in LIST; like `map` but don't return results |
| 448 | |
| 449 | ```scheme |
| 450 | (for-each (lambda (n) (print n)) '(3 4 5)) |
| 451 | ``` |
| 452 | |
| 453 | ## format |
| 454 | (format STRING ITEM ...): format the string with ITEMS as arguments |
| 455 | |
| 456 | ```scheme |
| 457 | (format "This uses formatting ~a ~s ~%" 'apple 'apple) |
| 458 | ``` |
| 459 | |
| 460 | ## func |
| 461 | |
| 462 | Turns a lambda into a Python function. |
| 463 | |
| 464 | (func (lambda ...)) |
| 465 | |
| 466 | ```scheme |
| 467 | (func (lambda (n) n)) |
| 468 | ``` |
| 469 | |
| 470 | ## There's more! |
| 471 | |
| 472 | Please see [Calysto Scheme Language](Calysto%20Scheme%20Language.ipynb) for more details on the Calysto Scheme language. |