You can see many examples using Calysto Scheme in IPython/Jupyter notebooks:

You can see many examples using Calysto Scheme in IPython/Jupyter notebooks:

Revision as of 05:23, 8 December 2014

Here we will provide documentation for using Calico and Calysto Scheme version 3.0.0

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.

atom?

boolean?

callback

(callback args body)

For use with event-oriented host APIs.

car, cdr and related

Given one of these functions and a list, it will return part of the list.

(car ...)

Examples:

(car '(a b)) => a
(car '(a . b)) => a
(car '((a) b) => (a)
(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)

(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)

define!

(define! variable value)

Define! is used to create variables in the hosting environment (Python or C#), whether they have a value or function bound to them. If you define! a function, Calico Scheme will automatically wrap it in a manner that is appropriate for the hosting environment. Note that this turns a Scheme function into a stack-based function in the host environment.

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.

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.

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.

You can use the datatypes in any number of ways, included the "cases" special form. For example, here is an un-parse function that takes the datatypes defined above, and turns it make into its original form:

choose/fail/cut

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:

;; Baker, Cooper, Fletcher, Miller, and Smith live on different floors of an
;; apartment house that contains only five floors. Baker does not live on the
;; top floor. Cooper does not live on the bottom floor. Fletcher does not
;; live on either the top or the bottom floor. Miller lives on a higher floor
;; than does Cooper. Smith does not live on a floor adjacent to Fletcher's.
;; Fletcher does not live on a floor adjacent to Cooper's. Where does
;; everyone live?

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 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.

Under The Hood

Calico Scheme is written in Scheme, in continuation-passing style (CPS), transformed into simpler forms, and then translated into C# and Python. Language-native components are written in Scheme.py (for Python) and Scheme.cs (for C#).

That is, whenever you have a result, pass it to a function that takes the result, and does further processing to it. The process is started with a TOP-k continuation that signals to halt and return the result.