Difference between revisions of "Calico Scheme"

From IPRE Wiki
Jump to: navigation, search
Line 1,686: Line 1,686:
 
=== call/cc ===
 
=== call/cc ===
  
(COMMAND ...)
+
(call/cc procedure)
  
Example:
+
Examples:
  
 
<pre>
 
<pre>
Line 1,736: Line 1,736:
  
 
=== try, catch, raise, finally ===
 
=== try, catch, raise, finally ===
 
(COMMAND ...)
 
  
 
Examples:
 
Examples:
Line 1,793: Line 1,791:
 
=== or ===
 
=== or ===
  
(COMMAND ...)
+
(or  items)
  
 
Example:
 
Example:
Line 1,803: Line 1,801:
 
</pre>
 
</pre>
  
=== COMMAND ===
+
=== case ===
  
(COMMAND ...)
+
(case ...)
  
 
Example:
 
Example:
Line 1,813: Line 1,811:
 
</pre>
 
</pre>
  
=== COMMAND ===
+
=== record-case ===
  
(COMMAND ...)
+
(record-case ...)
  
 
Example:
 
Example:
Line 1,823: Line 1,821:
 
</pre>
 
</pre>
  
=== COMMAND ===
+
=== raise ===
 
+
(COMMAND ...)
+
 
+
Example:
+
 
+
<pre>
+
</pre>
+
 
+
=== COMMAND ===
+
 
+
(COMMAND ...)
+
 
+
Example:
+
 
+
<pre>
+
</pre>
+
 
+
=== COMMAND ===
+
 
+
(COMMAND ...)
+
 
+
Example:
+
 
+
<pre>
+
</pre>
+
 
+
=== COMMAND ===
+
 
+
(COMMAND ...)
+
 
+
Example:
+
 
+
<pre>
+
</pre>
+
 
+
=== COMMAND ===
+
 
+
(COMMAND ...)
+
 
+
Example:
+
 
+
<pre>
+
</pre>
+
 
+
=== COMMAND ===
+
 
+
(COMMAND ...)
+
 
+
Example:
+
 
+
<pre>
+
</pre>
+
 
+
=== COMMAND ===
+
 
+
(COMMAND ...)
+
 
+
Example:
+
 
+
<pre>
+
</pre>
+
 
+
=== COMMAND ===
+
 
+
(COMMAND ...)
+
 
+
Example:
+
 
+
<pre>
+
</pre>
+
 
+
=== COMMAND ===
+
 
+
(COMMAND ...)
+
 
+
Example:
+
 
+
<pre>
+
</pre>
+
 
+
=== COMMAND ===
+
 
+
(COMMAND ...)
+
 
+
Example:
+
 
+
<pre>
+
</pre>
+
 
+
=== COMMAND ===
+
 
+
(COMMAND ...)
+
 
+
Example:
+
 
+
<pre>
+
</pre>
+
 
+
=== COMMAND ===
+
 
+
(COMMAND ...)
+
 
+
Example:
+
 
+
<pre>
+
</pre>
+
 
+
=== COMMAND ===
+
  
(COMMAND ...)
+
(raise ...)
  
 
Example:
 
Example:
Line 1,941: Line 1,831:
 
</pre>
 
</pre>
  
=== COMMAND ===
+
=== div ===
  
(COMMAND ...)
+
(div ...)
  
 
Example:
 
Example:

Revision as of 22:04, 13 April 2014

Here we will provide documentation for using Calico Scheme.

Calico Scheme is a new implementation of a Scheme-based language for Calico. It implements many core Scheme functions, but also adds some functionality to bring it into line with the other modern languages like Python and Ruby. In can run inside Calico as a DLL, or can run in Python.

Contents

Scheme Extensions

  1. Exceptions - implements try/catch/finally
  2. Modules and namespaces - implements namespaces for imports and foreign objects
  3. import libraries written for the Python, or the CLR
  4. Interop - ways for Scheme to interop with other Calico languages

Commands

quote, quasiquote, and unquote

(quote <i>item</i>)
'item

A quoted item (represented by a single-quote) is a symbol (if item is an atom), or is a list of quoted items if item is a list. Literals that are quoted are just the literals.

Examples:

'symbol => symbol
'(a list of items) => (a list of items)
(quasiquote <i>item</>)
`item

Quasiquote (represented by a back-quote) is like a regular quoted item; however, with a quasiquote, unquoted (represented with a comma) items are evaluated.

Examples:

(quasiquote (list (unquote (+ 1 2)) 4)) => (list 3 4)
`(list ,(+ 1 2) 4) => (list 3 4)

Notice that the (+ 1 2) is evaluated.

%

(% ...)

Modulo gives the amount left over after a divide.

Example:

(% 10 3) => 1

*

(* ...)

Multiply will take a number of arguments and give you the total of all numbers multiplied with each other.

Example:

(*) => 1
(* 12) => 12
(* 2 3) => 6
(* 2 3 4) => 24

+

(+ ...)

Addition will take a number of arguments and give you the sum of all of the numbers added together.

Example:

(+ 7 8) => 15

-

(- ...)

Subtraction will subtract one number from another.

Example:

(- 5 2) => 3

/

(/ ...)

Divide will divide one number from the previous.

Example:

(/) => 1
(/ 2) => 1/2
(/ 3 4) => 3/4

<

(< ...)

Less than will return #t or #f as to whether the first number is less than the second.

Example:

(< 5 2) => #f

<=

(<= ...)

Less than or equal than will return #t or #f as to whether the first number is less than, or equal to, the second.

Example:

(<= 5 6) => #t

=

(= ...)

Equality for numbers.

Example:

(= 6 7) => #f

>

(> ...)

Greater than will return #t or #f as to whether the first number is greater than the second.

Example:

(> 9 2) => #t

>=

(>= ...)

Greater than or equals will return #t or #f as to whether the first number is greater than or equal to the second.

Example:

(>= 4 5) => #f

abs

(abs ...)

Returns the absolute value of the number.

Example:

(abs -1) => 1

and

(and ...)

And will return #f as soon as it encounters a #f in the items. If no #f is encountered, it returns the last item.

Example:

(and 4 1 2 #t (quote ()) 0) => 0
(and 4 1 2 #t '() 3) => 3
(and 4 1 2 #f '() 0) => #f

append

(append ...)

Append takes a number of lists and combines them. The last item can be an atom, in which case it returns an improper list.

Examples:

(append (quote (1 2 3)) (quote (4 5 6))) => (1 2 3 4 5 6)
(append '(1 2 3) '(4 5 6)) => (1 2 3 4 5 6)
(append '(1 2 3) '(4 5 6) '(7 8 9)) => (1 2 3 4 5 6 7 8 9)
(append '(1 2 3) '(4 5 6) 7) => (1 2 3 4 5 6 . 7)

apply

(apply ...)

Example:

(apply car (quote ((1)))) => 1
(apply car '((1))) => 1

assq

(assq ...)

Example:

(assq 1 (quote ((1 2) (3 4)))) => (1 2)
(assq 1 '((1 2) (3 4))) => (1 2)

assv

(assv ...)

Example:

(assv 1 (quote ((1 2) (3 4)))) => (1 2)
(assv 1 '((1 2) (3 4))) => (1 2)

atom?

(atom? ...)

Example:

(atom? 1) => #t

boolean?

(boolean? ...)

Example:

(boolean? #t) => #t

car, cdr and related

(caaaar ...)

Example:

(caaaar (quote (((((hello there) this is a test) what is this) another item) in the list))) => (hello there)
(caaaar '(((((hello there) this is a test) what is this) another item) in the list)) => (hello there)

(caaadr ...)

Example:

(caaadr (quote (((((hello there) this is a test) what is this) another item) ((((((1 2 3) 4 5 6) 7 8 9) 10 11 12) 13 14 15) 16 17 18)))) => ((((1 2 3) 4 5 6) 7 8 9) 10 11 12)

(caaar ...)

Example:

(caaar (quote (((((hello there) this is a test) what is this) another item) in the list))) => ((hello there) this is a test)

(caadar ...)

Example:

(caadar (quote (((((hello there) this is a test) what is this) (((1 2 3) 4 5 6) 7 8 9) another item) in the list))) => ((1 2 3) 4 5 6)

(caaddr ...)

Example:

(caaddr (quote (((((hello there) this is a test) what is this) (((1 2 3) 4 5 6) 7 8 9) another item) head ((1 2) 3 4) in the list))) => (1 2)

(caadr ...)

Example:

(caadr (quote (((((hello there) this is a test) what is this) (((1 2 3) 4 5 6) 7 8 9) another item) (in this) ((7 8)) the list))) => in

(caar ...)

Example:

(caar (quote (((((hello there) this is a test) what is this) another item) in the list))) => (((hello there) this is a test) what is this)

(cadaar ...)

Example:

(cadaar (quote (((((hello there) this is a test) (what) is this) (yet another) item) in the list))) => (what)

(cadadr ...)

Example:

(cadadr (quote (((((hello there) this is a test) what is this) (yet another) item) (in the) list))) => the

(cadar ...)

Example:

(cadar (quote (((((hello there) this is a test) what is this) (yet another) item) in the list))) => (yet another)

(caddar ...)

Example:

(caddar (quote (((((hello there) this is a test) what is this) another item) in the list))) => item

(cadddr ...)

Example:

(cadddr (quote (((((hello there) this is a test) what is this) another item) in the list))) => list

(caddr ...)

Example:

(caddr (quote (((((hello there) this is a test) what is this) another item) in the list))) => the

(cadr ...)

Example:

(cadr (quote (((((hello there) this is a test) what is this) another item) in the list))) => in

(car ...)

Example:

(car (quote (((((hello there) this is a test) what is this) another item) in the list))) => ((((hello there) this is a test) what is this) another item)

(cdaaar ...)

Example:

(cdaaar (quote (((((hello there) this is a test) what is this) another item)))) => (this is a test)

(cdaadr ...)

Example:

(cdaadr (quote (((((hello there) this is a test) what is this) another item) ((7 8)) 9 10))) => (8)

(cdaar ...)

Example:

(cdaar (quote (((((hello there) this is a test) what is this) another item)))) => (what is this)

(cdadar ...)

Example:

(cdadar (quote (((((hello there) this is a test) what is this) (another two) items)))) => (two)

(cdaddr ...)

Example:

(cdaddr (quote (((((hello there) this is a test) what is this) another item) 1 (2 5) 3 4))) => (5)

(cdadr ...)

Example:

(cdadr (quote (((((hello there) this is a test) what is this) another item) (1 6) (2 5) 3 4))) => (6)

(cdar ...)

Example:

(cdar (quote (((((hello there) this is a test) what is this) another item)))) => (another item)

(cddaar ...)

Example:

(cddaar (quote (((((hello there) this is a test) what is this) another item) 1 (2) 3))) => (is this)

(cddadr ...)

Example:

(cddadr (quote (((((hello there) this is a test) what is this) another item) (7 13) (8 12) 9 10))) => ()

(cddar ...)

Example:

(cddar (quote (((((hello there) this is a test) what is this) another item)))) => (item)

(cdddar ...)

Example:

(cdddar (quote (((((hello there) this is a test) what is this) another item)))) => ()

(cddddr ...)

Example:

(cddddr (quote (((((hello there) this is a test) what is this) another item) 1 2 3 4 5))) => (4 5)

(cdddr ...)

Example:

(cdddr (quote (((((hello there) this is a test) what is this) another item) 1 2 3 4))) => (3 4)

(cddr ...)

Example:

(cddr (quote (((((hello there) this is a test) what is this) another item) 1 2 3))) => (2 3)

(cdr ...)

Example:

(cdr (quote (((((hello there) this is a test) what is this) another item) 1 2 3))) => (1 2 3)

case

(case ...)

Example:

(case (quote thing1) (thing2 1) (thing1 2)) => 2
(case (quote thing1) (thing2 1) ((thing1 thing3) 2)) => 2
(case (quote thingx) (thing2 1) ((thing1 thing3) 2) (else 3)) => 3

cd

(cd ...)

Example:

(cd) => ""

char->integer

(char->integer ...)

Example:

(char->integer #\a) => 97

char->string

(char->string ...)

Example:

(char->string #\b) => "b"

char-alphabetic?

(char-alphabetic? ...)

Example:

(char-alphabetic? #\A) => #t

char-numeric?

(char-numeric? ...)

Example:

(char-numeric? #\1) => #t

char-whitespace?

(char-whitespace? ...)

Example:

(char-whitespace? #\t) => #f

char-whitespace?

(char-whitespace? ...)

Example:

(char-whitespace? #\	) => #t

char-whitespace?

(char-whitespace? ...)

Example:

(char-whitespace? #\
) => #t

char-whitespace?

(char-whitespace? ...)

Example:

(char-whitespace? #\a) => #f

char=?

(char=? ...)

Example:

(char=? #\a #\a) => #t

char=?

(char=? ...)

Example:

(char=? #\a #\b) => #f

char?

(char? ...)

Example:

(char? 2) => #f

cond

(cond ...)

Example:

(cond (#f 1) (else 2)) => 2

cons

(cons ...)

Example:

(cons 1 (quote ())) => (1)

current-directory

(current-directory ...)

Example:

(current-directory) => "."

current-environment

(current-environment ...)

Example:

(length (dir (current-environment))) => 160

current-time

(current-time ...)

Example:

(current-time) => 1397405584.229055

cut

(cut ...)

The (cut) operation will succeed, but cannot be back-tracked afterwards.

Example:

(letrec ((loop (lambda (n) (if (= n 0) (set! var (cut 23)) (loop (- n 1))))) (var 0)) (loop 10) var) => (23)

dict

(dict ...)

Example:

(dict (quote ((1 2) (3 4)))) => none

dir

(dir ...)

Object properties and enviroment variables.

==> (dir)
(- % * / + < <= = =? > >= abort abs and append apply assq assv atom? boolean? caaaar caaadr caaar caadar 
caaddr caadr caar cadaar cadadr cadar caddar cadddr caddr cadr call/cc call-with-current-continuation 
car case cases cd cdaaar cdaadr cdaar cdadar cdaddr cdadr cdar cddaar cddadr cddar cdddar cddddr cdddr 
cddr cdr char? char=? char-alphabetic? char-numeric? char-whitespace? cond cons current-directory 
current-environment current-time cut debug define-datatype dir display eq? equal? eqv? error eval 
eval-ast even? exit float for-each format get get-member globals import int iter? length let let* letrec 
list list? list->string list->vector list-head list-ref list-tail load make-set make-vector map member 
memq memv newline not null? number? number->string odd? or pair? parse parse-string print printf 
procedure? property quotient range rational read-string record-case remainder require reset-toplevel-env 
reverse safe-print set-car! set-cdr! sort sqrt string string? string<? string=? string->list string->number 
string->symbol string-append string-length string-ref string-split substring symbol symbol? symbol->string 
typeof unparse unparse-procedure use-lexical-address use-tracing using vector vector? vector->list 
vector-ref vector-set! void zero?)
==> (dir my-stuff)
(x y z)

You can also use dir on an object:

(using "Myro")
(dir Myro)

Example:

(length (dir)) => 170

eq?

(eq? ...)

Example:

(eq? (quote a) (quote a)) => #t

equal?

(equal? ...)

Example:

(equal? 1 1.0) => #t

eqv?

(eqv? ...)

Example:

(eqv? 1 1) => #t

error

(error ...)

Example:

(try (error (quote a) "message") (catch e e (cadr e))) => "Error in 'a': message"

eval

(eval ...)

Example:

(eval (quote (+ 1 2))) => 3

eval-ast

(eval-ast ...)

Example:

(eval-ast (parse (quote (+ 3 4)))) => 7

even?

(even? ...)

Example:

(even? 33) => #f

float

(float ...)

Example:

(float 23) => 23.0

for-each

(for-each ...)

Example:

(for-each (lambda (n) (+ n 1)) (quote (1 2 3))) => <void>

format

(format ...)

Example:

(format "~a ~s ~%" "hello" "hello") => "hello \"hello\" 
"

get-stack-trace

(get-stack-trace ...)

Example:

(caddr (cadar (get-stack-trace))) => 69

Modules

Modules provide an easy method for structuring hierarchies of libraries and code.

Import

(import <string-exp>)
(import <string-exp> '<symbol-exp>)

Examples

==> (import "my-file.ss")

my-file.ss is a Scheme program file, which itself could have imports.

==> (import "my-file.ss" 'my-stuff)

Loads the file, and puts it in the namespace "my-stuff" accessible through the lookup interface below.

import uses the local environment, while load uses the toplevel-environment.

Lookup

module.name
module.module.name
==> (import "my-file.ss" 'my-stuff)
==> my-stuff.x
5

int

(int ...)

Example:

(int 12.8) => 13

integer->char

(integer->char ...)

Example:

(integer->char 97) => #\a

iter?

(iter? ...)

Example:

(iter? 3) => #f

length

(length ...)

Example:

(length (quote (1 2 3))) => 3

let

(let ...)

Example:

(let ((x 1)) x) => 1

let*

(let* ...)

Example:

(let* ((x 1) (y (+ x 1))) y) => 2

letrec

(letrec ...)

Example:

(letrec ((loop (lambda (n) (if (= n 0) (quote ok) (loop (- n 1)))))) (loop 10)) => ok

list

(list ...)

Example:

(list 1 2) => (1 2)

list->string

(list->string ...)

Example:

(list->string (quote (#\1 #\2 #\3))) => "123"

list->vector

(list->vector ...)

Example:

(list->vector (quote (1 2 3))) => [1, 2, 3]

list-ref

(list-ref ...)

Example:

(list-ref (quote (1 2 3)) 1) => 2

list?

(list? ...)

Example:

(list? (quote (1 2 3))) => #t

make-set

(make-set ...)

Example:

(sort < (make-set (quote (1 2 3 1 2)))) => (1 2 3)

make-vector

(make-vector ...)

Example:

(make-vector 3) => [0, 0, 0]

map

(map ...)

Example:

(map (lambda (n) (+ n 1)) (range 5)) => (1 2 3 4 5)

member

(member ...)

Example:

(member "b" (quote ("a" "b" "c"))) => ("b" "c")

memq

(memq ...)

Example:

(memq (quote b) (quote (a b c))) => (b c)

memv

(memv ...)

Example:

(memv 2 (quote (1.0 2.0 3.0))) => (2.0 3.0)

not

(not ...)

Example:

(not #f) => #t

null?

(null? ...)

Example:

(null? (quote ())) => #t

number->string

(number->string ...)

Example:

(number->string 23) => "23"

number?

(number? ...)

Example:

(number? 23) => #t

odd?

(odd? ...)

Example:

(odd? 45) => #t

or

(or ...)

Example:

(or #t (/ 1 0)) => #t

pair?

(pair? ...)

Example:

(pair? (quote ())) => #f

pair?

(pair? ...)

Example:

(pair? (cons 1 2)) => #t

parse

(parse ...)

Example:

(parse (quote (+ 1 2))) => (app-aexp (lexical-address-aexp 0 1 + none) ((lit-aexp 1 none) (lit-aexp 2 none)) none)

parse-string

(parse-string ...)

Example:

(parse-string "(- 7 8)") => (app-aexp (lexical-address-aexp 0 2 - (stdin 1 2 2 1 2 2)) ((lit-aexp 7 (stdin 1 4 4 1 4 4)) (lit-aexp 8 (stdin 1 6 6 1 6 6))) (stdin 1 1 1 1 7 7))

procedure?

(procedure? ...)

Example:

(procedure? procedure?) => #t

quotient

(quotient ...)

Example:

(quotient 1 4) => 0

rac

(rac ...)

Example:

(rac (quote (1 2 3))) => 3

range

(range ...)

Example:

(range 10) => (0 1 2 3 4 5 6 7 8 9)

rational

(rational ...)

Example:

(rational 3 4) => 3/4

rdc

(rdc ...)

Example:

(rdc (quote (1 2 3))) => (1 2)

read-string

(read-string ...)

Example:

(read-string (quote (1 2 3))) => ((pair) ((atom) 1 (stdin 1 2 2 1 2 2)) ((pair) ((atom) 2 (stdin 1 4 4 1 4 4)) ((pair) ((atom) 3 (stdin 1 6 6 1 6 6)) ((atom) () none) none) none) (stdin 1 1 1 1 7 7))

remainder

(remainder ...)

Example:

(remainder 1 4) => 1

require

(require ...)

Example:

(require #t) => ok

reverse

(reverse ...)

Example:

(reverse (quote (1 2 3))) => (3 2 1)

round-1

(round-1 ...)

Example:

(round 45.5) => 46

round-2

(round-2 ...)

Example:

(round 45.4) => 45

set-car!

(set-car! ...)

Example:

(let ((x (quote (1 2 3)))) (set-car! x 0) x) => (0 2 3)

set-cdr!

(set-cdr! ...)

Example:

(let ((x (quote (1 2 3)))) (set-cdr! x (quote (3 4))) x) => (1 3 4)

snoc

(snoc ...)

Example:

(snoc 0 (quote (1 2 3))) => (1 2 3 0)

sort

(sort ...)

Example:

(sort < (quote (3 7 1 2))) => (1 2 3 7)

sqrt

(sqrt ...)

Example:

(sqrt 3) => 1.7320508075688772

string

(string ...)

Example:

(string #\1 #\2) => "12"

string->list

(string->list ...)

Example:

(string->list "hello world") => (#\h #\e #\l #\l #\o #\  #\w #\o #\r #\l #\d)

string->number

(string->number ...)

Example:

(string->number "12.1") => 12.1

string->symbol

(string->symbol ...)

Example:

(string->symbol "hello") => hello

string-append

(string-append ...)

Example:

(string-append "hell" "o") => "hello"

string-length

(string-length ...)

Example:

(string-length "what") => 4

string-ref

(string-ref ...)

Example:

(string-ref "what" 2) => #\a

string-split

(string-split ...)

Example:

(string-split "hello.world" #\.) => ("hello" "world")

string<?

(string<? ...)

Example:

(string<? "a" "b") => #t

string=?

(string=? ...)

Example:

(string=? "a" "b") => #f

string?

(string? ...)

Example:

(string? "hello") => #t

substring

(substring ...)

Example:

(substring "hello" 1 3) => "el"

symbol

(symbol ...)

Example:

(symbol "hello") => hello

symbol->string

(symbol->string ...)

Example:

(symbol->string (quote hello)) => "hello"

symbol?

(symbol? ...)

Example:

(symbol? (quote hello)) => #t

typeof

(typeof ...)

Example:

(typeof 23) => <type 'int'>

unparse

(unparse ...)

Example:

(unparse (parse (quote (+ 1 2)))) => (+ 1 2)

use-lexial-address

(use-lexial-address ...)

Example:

(use-lexical-address) => #t

use-satck-trace

(use-satck-trace ...)

Example:

(use-stack-trace) => #t

use-tracing

(use-tracing ...)

Example:

(use-tracing) => #f

using

(using ...)

Example:

(try (using "math") (catch e e (using "Graphics"))) => ()

vector

(vector ...)

Example:

(vector 1 2 3) => [1, 2, 3]

vector->lsit

(vector->lsit ...)

Example:

(vector->list (vector 1 2 3)) => (1 2 3)

vector-ref

(vector-ref ...)

Example:

(vector-ref (vector 1 2 3) 2) => 3

let

(let ...)

Example:

(let ((v (vector 1 2 3))) (vector-set! v 2 (quote a)) v) => [1, 2, a]

vector?

(vector? ...)

Example:

(vector? (vector)) => #t

(void)

((void) ...)

Example:

(void) => <void>

zero?

(zero? ...)

Example:

(zero? 0.0) => #t

my-odd

(my-odd ...)

Example:

(my-odd? 42) => #f

my-even

(my-even ...)

Example:

(my-even? 42) => #t

my-odd

(my-odd ...)

Example:

(my-odd? 43) => #t

my-even

(my-even ...)

Example:

(my-even? 43) => #f


(begin (define hello 0) (for 5 times do (set! hello (+ hello 1))) hello) => 5
(for sym in (quote (a b c d)) do (define x 1) (set! x sym) x) => done
(for n in (range 10 20 2) do n) => done
(for n at (i j) in matrix2d do (list n (quote coords:) i j)) => done
(for n at (i j k) in matrix3d do (list n (quote coords:) i j k)) => done
(! 5) => 120
(nth 10 facts) => 3628800
(nth 20 fibs) => 10946
(first 30 fibs) => (1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040)

lambda

(lambda ...)

Example:

((lambda x x) 1 2 3 4 5) => (1 2 3 4 5)
((lambda (x . y) (list x y)) 1 2 3 4 5) => (1 (2 3 4 5))
((lambda (a b . z) (list a b z)) 1 2 3 4 5) => (1 2 (3 4 5))
((lambda (a b . z) (list a b z)) 1 2 3) => (1 2 (3))
((lambda (a b . z) (list a b z)) 1 2) => (1 2 ())
(try ((lambda (a b . z) (list a b z)) 1) (catch e e "not enough arguments given")) => "not enough arguments given"

call/cc

(call/cc procedure)

Examples:

(* 10 (call/cc (lambda (k) 4))) => 40
(* 10 (call/cc (lambda (k) (+ 1 (k 4))))) => 40
(* 10 (call/cc (lambda (k) (+ 1 (call/cc (lambda (j) (+ 2 (j (k 5))))))))) => 50
(* 10 (call/cc (lambda (k) (+ 1 (call/cc (lambda (j) (+ 2 (k (j 5))))))))) => 60

Exceptions

(raise <exp>)
(try <exp>...)
(try <exp>... (catch <sym> <exp>...))
(try <exp>... (finally <exp>...))
(try <exp>... (catch <sym> <exp>...) (finally <exp>...))

There are four main forms of try:

  1. try alone: has no effect---doesn't catch exceptions
  2. try with a catch-clause: if exception in try-body, catch-clause will catch and provide return value, otherwise body of try provides. If exception in catch-clause will simple raise it.
  3. try with a finally-clause: finally-clause will run if exception in try-body or not. If there is an exception in the finally, it will raise it.
  4. try with a catch-clause and finally-clause: if an exception in try-body, the catch-clause will run, followed by the finally-clause. If an exception is raised in the catch-clause too, then finally-clause will run, and then raise the catch-exception; if there is an exception raised in the

finally-clause, then it will raise.

The <sym> of the catch-clause will be bound to the expression raised.

Examples

==> 

> (try x)
(uncaught exception: "unbound variable x")
==> "(try x (catch e e))"
"unbound variable x"
==> "(try x (catch e (raise e)))"
(uncaught exception: "unbound variable x")
==> "(try x (finally 'hi))"
hi 
(uncaught exception: "unbound variable x")
==> "(try x (catch e 1 2 3 4 (finally 'hi))"
hi 
4

try, catch, raise, finally

Examples:

(try (let loop ((n 5)) n (if (= n 0) (raise (quote blastoff!))) (loop (- n 1))) (catch e e)) => blastoff!

(try 3) => 3

(try 3 (finally (quote yes) 4)) => 3

(try (raise (quote yes)) (catch e e)) => yes

(try (try (raise (quote yes))) (catch e e)) => yes

(try (try (begin (quote one) (raise (quote oops)) (quote two))) (catch e e)) => oops

(* 10 (try (begin (quote one) (raise (quote oops)) (quote two)) (catch ex 3 4))) => 40

(* 10 (try (begin (quote one) (quote two) 5) (catch ex 3 4))) => 50

(* 10 (try (begin (quote one) (raise (quote oops)) 5) (catch ex (list (quote ex:) ex) 4))) => 40

(try (* 10 (try (begin (quote one) (raise (quote oops)) 5) (catch ex (list (quote ex:) ex) (raise ex) 4))) (catch e e)) => oops

(try (* 10 (try (begin (quote one) (raise (quote oops)) 5) (catch ex (list (quote ex:) ex) (raise ex) 4) (finally (quote two) 7))) (catch e e)) => oops

(try (* 10 (try (begin (quote one) (raise (quote oops)) 5) (catch ex (list (quote ex:) ex) (raise (quote bar)) 4))) (catch x (quote hello) 77)) => 77

(try 3 (finally (quote hi) 4)) => 3

(try (div 10 0) (catch e e)) => "division by zero"

(try (let ((x (try (div 10 0)))) x) (catch e e)) => "division by zero"

(let ((x (try (div 10 2) (catch e -1)))) x) => 5

(let ((x (try (div 10 0) (catch e -1)))) x) => -1

(let ((x (try (div 10 2) (catch e -1) (finally (quote closing-files) 42)))) x) => 5

(let ((x (try (div 10 0) (catch e -1) (finally (quote closing-files) 42)))) x) => -1

(let ((x (try (div 10 2) (finally (quote closing-files) 42)))) x) => 5

(try (let ((x (try (div 10 0) (catch e -1 (raise (quote foo))) (finally (quote closing-files) 42)))) x) (catch e e)) => foo

(try (let ((x (try (div 10 0) (catch e -1 (raise (quote foo))) (finally (quote closing-files) (raise (quote ack)) 42)))) x) (catch e e)) => ack

(try (let ((x (try (div 10 0) (catch e -1 (raise (quote foo))) (finally (quote closing-files) (raise (quote ack)) 42)))) x) (catch e (if (equal? e (quote ack)) 99 (raise (quote doug)))) (finally (quote closing-outer-files))) => 99

(try (try (let ((x (try (div 10 0) (catch e -1 (raise (quote foo))) (finally (quote closing-files) (raise (quote ack)) 42)))) x) (catch e (if (equal? e (quote foo)) 99 (raise (quote doug)))) (finally (quote closing-outer-files))) (catch e e)) => doug

or

(or items)

Example:

(let ((bool 5)) (or (= bool 4) (= bool 5))) => #t
(let ((bool 5)) (or (= bool 4) 6)) => 6
(let ((bool 5)) (and (= bool 5) (> bool 0) (= bool 4))) => #f

case

(case ...)

Example:

(let ((r 5)) (case (quote banana) (apple (quote no)) ((cherry banana) 1 2 r) (else (quote no)))) => 5

record-case

(record-case ...)

Example:

(let ((r 5)) (record-case (cons (quote banana) (cons (quote orange) (cons (* 2 3) (quote ())))) (apple (a b c) (list c b a r)) ((cherry banana) (a . b) (list b a r)) ((orange) () (quote no)) (else 2 3 4))) => ((6) orange 5)

raise

(raise ...)

Example:

(define div (lambda (x y) (if (= y 0) (raise "division by zero") (/ x y)))) => <void>

div

(div ...)

Example:

(div 10 2) => 5

define-datatype

(define-datatype ...)

Example:

(define-datatype lc-exp lc-exp?
  (var-exp 
   (var symbol?))
  (lambda-exp 
   (bound-var symbol?)
   (body lc-exp?))
  (app-exp
   (rator lc-exp?)
   (rand lc-exp?)))

That defines the following:

lc-exp?
var-exp
lambda-exp
app-exp
(var-exp (quote a)) => ()
(lambda-exp (quote a) (var-exp (quote a))) => ()
(app-exp (lambda-exp (quote a) (var-exp (quote a))) (var-exp (quote a))) => ()
(define un-parse
  (lambda (exp)
    (cases lc-exp exp
       (var-exp (var) var)
       (lambda-exp (bound-var body) (list bound-var body))
       (app-exp (rator rand) (list rator rand)))))

(un-parse (var-exp (quote a))) => a
(un-parse (lambda-exp (quote a) (var-exp (quote a)))) => (a (var-exp a))
(un-parse (app-exp (lambda-exp (quote a) (var-exp (quote a))) (var-exp (quote a)))) => ((lambda-exp a (var-exp a)) (var-exp a))

choose/fail

Calico Scheme also contains a non-deterministic search with back-tracking. To use this, you select choice-points using the keyword "choose" with arguments, set requirements using the keyword "require", and fail using "(choose)". For example, to automatically find two numbers that sum to seven:

(define sum-to-seven
  (lambda ()
    (let ((num1 (choose 0 1 2 3 4 5 6 7 8 9))
          (num2 (choose 0 1 2 3 4 5 6 7 8 9)))
      (require (= (+ num1 num2) 7))
      (printf "The numbers are ~s ~s\n" num1 num2))))

Then, you can let Scheme do the searching for you:

scheme>>> (sum-to-seven)
The numbers are 0 7
Done

If you don't like that result, you can force Scheme back to any choice-points to make a different choice:

scheme>>> (choose)
The numbers are 1 6
Done

This can continue until there are no more choices left.

See menu -> File -> Examples -> Scheme -> choose-examples.ss for more examples.

scheme>>> (choose)
The numbers are 7 0
Done
(define distinct? 
  (lambda (nums) 
    (or (null? nums) 
	(null? (cdr nums)) 
	(and (not (member (car nums) (cdr nums))) 
	     (distinct? (cdr nums))))))
(define floors2 
  (lambda () 
    (let ((baker (choose 1 2 3 4 5))) 
      (require (not (= baker 5))) 
      (let ((fletcher (choose 1 2 3 4 5))) 
	(require (not (= fletcher 5))) 
	(require (not (= fletcher 1))) 
	(let ((cooper (choose 1 2 3 4 5))) 
	  (require (not (= cooper 1))) 
	  (require (not (= (abs (- fletcher cooper)) 1))) 
	  (let ((smith (choose 1 2 3 4 5))) 
	    (require (not (= (abs (- smith fletcher)) 1))) 
	    (let ((miller (choose 1 2 3 4 5))) 
	      (require (> miller cooper)) 
	      (require (distinct? (list baker cooper fletcher miller smith))) 
	      (list (list (quote baker:) baker) 
		    (list (quote cooper:) cooper) 
		    (list (quote fletcher:) fletcher) 
		    (list (quote miller:) miller) 
		    (list (quote smith:) smith)))))))))
(floors2) => ((baker: 3) (cooper: 2) (fletcher: 4) (miller: 5) (smith: 1))

define-syntax

define-syntax is used to change the semantics of Scheme in a manner not possible with regular functions. For example, imagine that you wanted to time a particular function call. To time a function, you can do:

(let ((start (current-time)))
  (fact 5)
  (- (current-time) start))

If you tried to define a function time such that you could call it like:

(time (fact 5))

then, unfortunately, you would evaluate (fact 5) before you could do anything in the function time. You could call it like:

(time fact 5)

but that looks a bit strange. Perhaps a more natural way would be to just change the semantics of Scheme to allow (time (fact 5)). Scheme makes that easy with define-sytnax:

(define-syntax time 
  [(time ?exp) (let ((start (current-time)))
		 ?exp
		 (- (current-time) start))])

Now, you can call it like:

(time (fact 5))

and you get the correct answer.

define-syntax takes a list of two items: a template, and a response. If the template matches, then you evaluate the response. In this example, (time ?exp) matches, so the system will record the start time, evaluate the ?exp, and then return the time minus the start time.

Calico Scheme uses this simple, but powerful pattern matcher to implement define-case. Here is a more complex example: for.

(define-syntax for
  [(for ?exp times do . ?bodies)
   (for-repeat ?exp (lambda () . ?bodies))]
  [(for ?var in ?exp do . ?bodies)
   (for-iterate1 ?exp (lambda (?var) . ?bodies))]
  [(for ?var at (?i) in ?exp do . ?bodies)
   (for-iterate2 0 ?exp (lambda (?var ?i) . ?bodies))]
  [(for ?var at (?i ?j . ?rest) in ?exp do . ?bodies)
   (for ?var at (?i) in ?exp do
     (for ?var at (?j . ?rest) in ?var do . ?bodies))])

In this example, define-syntax creates a for function with 4 forms:

(for 4 times do (function ...))

(for x in '(1 2 3) do (function ...))

(for x at (0) in '(1 2 3) do (function ...))

(for x at (0 1 2) in (range 10) do (function ...))
(define-syntax collect
  [(collect ?exp for ?var in ?list)
   (filter-map (lambda (?var) ?exp) (lambda (?var) #t) ?list)]
  [(collect ?exp for ?var in ?list if ?condition)
   (filter-map (lambda (?var) ?exp) (lambda (?var) ?condition) ?list)])

(collect (* n n) for n in (range 10)) => (0 1 4 9 16 25 36 49 64 81)

(collect (* n n) for n in (range 5 20 3)) => (25 64 121 196 289)

(collect (* n n) for n in (range 10) if (> n 5)) => (36 49 64 81)

Calico Libraries

You can use any of the Calico libraries:

scheme> (using "DLLName")
scheme> (DLLName.Class arg1 arg2)

For example, you can use Scheme to do art or control robots:

(using "Myro")
(Myro.init "sim")
(Myro.joystick)

Interop

There is a special object in the environment, calico. It has access to a variety of Calico functions. See Calico: calico object for more details.

Use the define! to put a variable in the global Calico namespace.

scheme> (define! x 8)
python> x
8

Wrap a function for use by other Calico languages:

scheme> (func (lambda (a b) (+ a b)))

Combine for cross-language interoperation:

 scheme> (define fact (lambda (n) (if (= n 1) 1 (* n (fact (- n 1))))))
 scheme> (define! factorial (func fact))
 python> factorial(5)
 120

Be careful not to wrap the func around the part that is called recursively, or you will destroy the tail-call optimization.

Iterators

Strings, vectors, and lists all work with map and for-each.

==> (map display "123")
123(void void void)
==> (for-each (lambda (v) (printf "~a\n" v)) (range 3))
1
2
3

Help/Doc System

When you define a variable, you can optionally add a doc-string:

==> (define x "This variable holds the sum" 0)
==> (define y 0)
==> (define function "This computes the polynomial..." (lambda (x) ...))

You can lookup the doc-string with:

==> (help 'x)
This variable holds the sum

Misc

typeof will give you the .NET type of a value:

==> (typeof 1)
System.Int32
==> (typeof 238762372632732736)
Microsoft.Scripting.Math.BigInteger
==> (typeof 1/5)                
Rational
==> (cd "/path/to/folder")
==> (cd)
"/path/to/folder"
==> (import "file.ss" 'F)
==> (set! F.x 45)
==> (current-environment)
#<environment>
==> (remainder 4 5)
==> (quotient 6 4)
==> (dir (current-environment))
==> (make-set '(1 1 2 3 4 5))
==> (using "Myro")
==> (dir Myro.robot)

Language Interop

Converting from Scheme to Python:

scheme> (define pylist (calico.Evaluate "lambda *args: list(args)" "python"))
Ok
scheme> (define pytuple (calico.Evaluate "lambda *args: args" "python"))
Ok
scheme> (pylist 1 2 3)
[1, 2, 3]
scheme> (pytuple 1 2 3)
(1, 2, 3)

Converting from Python to Scheme

scheme> (define pylist2list
           (lambda (args)
              (map (lambda (i) i) args)))
Ok
scheme> (pylist2list (pylist 1 2 3))
(1 2 3)
scheme> (pylist2list (pytuple 1 2 3))
(1 2 3)

References

Calico

  1. CalicoDevelopment - plans and details for Calico development
  2. http://www.scheme.com/tspl4/ - The Scheme Programming Language, 4th edition

For Developers