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.
In Jupyter notebooks, because Calysto Scheme uses MetaKernel, 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.
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/src/Scheme.py However, you can easily install it (see below).
You can install Calysto Scheme with Python3:
pip3 install --upgrade calysto-scheme --user -U
python3 -m calysto_kernel install --user
or in the system kernel folder with:
sudo pip3 install --upgrade calysto-scheme -U
sudo python3 -m calysto_kernel install
Change pip3/python3 to use a different pip or Python. The version of Python used will determine how Calysto Scheme is run.
Use it in the console, qtconsole, or notebook with IPython 3:
ipython console --kernel calysto_scheme
ipython qtconsole --kernel calysto_scheme
ipython notebook --kernel calysto_scheme
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.
When you run Calysto Scheme in Jupyter (console, notebook, qtconsole, etc.) you get:
Calysto Scheme allows you to use LaTeX-style variables in code. For example, if you type:
\beta
with the cursor right after the 'a' in beta, and then press TAB, it will turn into the unicode character:
β
There are nearly 1300 different symbols defined (thanks to the Julia language) and documented here:
http://docs.julialang.org/en/release-0.4/manual/unicode-input/#man-unicode-input
Calysto Scheme may not implement all of those. Some useful and suggestive ones:
In [1]:
(define α 67)
In [2]:
α
Out[2]:
In [6]:
(define i 2)
(define vectorᵢ (vector-ref (vector 0 6 3 2) i))
vectorᵢ
Out[6]:
In [2]:
(import "calysto.display")
Out[2]:
In [3]:
(calysto.display.HTML "This is <b>bold</b>, <i>italics</i>, <u>underlined</u>.")
Out[3]:
In [13]:
(import "calysto.graphics")
Out[13]:
In [14]:
(define canvas (calysto.graphics.Canvas))
In [15]:
(define ball (calysto.graphics.Circle '(150 150) 100))
In [16]:
(ball.draw canvas)
Out[16]:
In [3]:
! ls /tmp
%%debug
(begin
(define x 1)
(set! x 2)
)
In [10]:
(python-eval "1 + 2")
Out[10]:
In [1]:
(python-exec
"
def mypyfunc(a, b):
return a * b
")
This is a shared environment with Scheme:
In [2]:
(mypyfunc 4 5)
Out[2]:
You can use func
to turn a Scheme procedure into a Python function, and define!
to put it into the shared enviornment with Python:
In [4]:
(define! mypyfunc2 (func (lambda (n) n)))
In [5]:
(python-eval "mypyfunc2(34)")
Out[5]:
choose
Calysto Scheme acts as if it has a call stack, for easier debugging. For example:
In [4]:
(define fact
(lambda (n)
(if (= n 1)
q
(* n (fact (- n 1))))))
In [5]:
(fact 5)
To turn off the stack trace on error:
(use-stack-trace #f)
That will allow infinite recursive loops without keeping track of the "stack".
In [6]:
SCHEMEPATH
Out[6]:
In [7]:
(set-cdr! (cdr SCHEMEPATH) (list "/var/modules"))
In [8]:
SCHEMEPATH
Out[8]:
In [9]:
(% 5 2)
Out[9]:
In [10]:
(* 2 3 4 5)
Out[10]:
In [11]:
(+ 1 1 1)
Out[11]:
In [12]:
(- 5 4 2)
Out[12]:
In [13]:
(/ 3 2)
Out[13]:
In [14]:
(// 3 2)
Out[14]:
In [15]:
(< 6 7)
Out[15]:
In [16]:
(<= 5.5 5)
Out[16]:
In [17]:
(= 7 8)
Out[17]:
In [18]:
(> 8 6)
Out[18]:
In [19]:
(>= 9 9)
Out[19]:
In [20]:
(abort)
In [21]:
(abs -67)
Out[21]:
In [22]:
(and #f (/ 6 0))
Out[22]:
In [23]:
(append '(1 2 3) '(4 5))
Out[23]:
In [24]:
(apply + '(1 2 3))
Out[24]:
In [25]:
(assq 'key '((apple 1)(key 2)))
Out[25]:
In [26]:
(assq 'key '((apple 1)(banana 2)))
Out[26]:
In [27]:
(assv 'key '((apple 1)(key 2)))
Out[27]:
In [28]:
(atom? (cons 1 2))
Out[28]:
In [29]:
(atom? 'a)
Out[29]:
In [30]:
(boolean? #t)
Out[30]:
In [31]:
(caaaar '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[31]:
In [32]:
(caaadr '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[32]:
In [33]:
(caaar '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[33]:
In [34]:
(caadar '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[34]:
In [35]:
(caaddr '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[35]:
In [36]:
(caadr '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[36]:
In [37]:
(caar '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[37]:
In [38]:
(cadaar '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[38]:
In [39]:
(cadadr '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in) another) (the) list))
Out[39]:
In [40]:
(cadar '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[40]:
In [41]:
(caddar '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[41]:
In [42]:
(cadddr '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[42]:
In [43]:
(caddr '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[43]:
In [44]:
(cadr '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[44]:
In [45]:
(* 10 (call/cc (lambda (k) 4)))
Out[45]:
In [46]:
(* 10 (call/cc (lambda (k) (+ 1 (k 4)))))
Out[46]:
In [47]:
(* 10 (call/cc (lambda (k)
(+ 1 (call/cc
(lambda (j)
(+ 2 (j (k 5)))))))))
Out[47]:
In [48]:
(* 10 (call/cc (lambda (k)
(+ 1 (call/cc
(lambda (j)
(+ 2 (k (j 5)))))))))
Out[48]:
In [49]:
(car '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[49]:
In [50]:
(case 'thing1 (thing2 1) (thing1 2))
Out[50]:
In [51]:
(case 'thing1 (thing2 1) ((thing1 thing3) 2))
Out[51]:
In [52]:
(case 'thingx (thing2 1) ((thing1 thing3) 2) (else 3))
Out[52]:
In [53]:
(case 'banana
(apple 'no)
((cherry banana) 1 2 3)
(else 'no))
Out[53]:
In [54]:
(cd "/")
Out[54]:
In [55]:
(cdaaar '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[55]:
In [56]:
(cdaadr '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[56]:
In [57]:
(cdaar '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[57]:
In [58]:
(cdadar '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[58]:
In [59]:
(cdaddr '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[59]:
In [60]:
(cdadr '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[60]:
In [61]:
(cdar '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[61]:
In [62]:
(cddaar '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[62]:
In [63]:
(cddadr '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in) some other) (the) list))
Out[63]:
In [64]:
(cddar '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[64]:
In [65]:
(cdddar '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[65]:
In [66]:
(cddddr '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[66]:
In [67]:
(cdddr '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[67]:
In [68]:
(cddr '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[68]:
In [69]:
(cdr '(((((hello there) this is a test) what is this)
(((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))
Out[69]:
In [70]:
(char->integer #\1)
Out[70]:
In [71]:
(char->string #\a)
Out[71]:
In [72]:
(char-alphabetic? #\1)
Out[72]:
In [73]:
(char-alphabetic? #\a)
Out[73]:
In [74]:
(char-numeric? #\2)
Out[74]:
In [75]:
(char-whitespace? #\tab)
Out[75]:
In [76]:
(char=? #\1 #\2)
Out[76]:
In [77]:
(char? "h")
Out[77]:
In [21]:
(define x (choose 1 2 3))
In [22]:
x
Out[22]:
In [23]:
(require #f)
In [24]:
x
Out[24]:
In [25]:
(require #f)
In [26]:
x
Out[26]:
In [27]:
(require #f)
Out[27]:
In [28]:
x
Out[28]:
You can solve many problems in a concise manner with choose/require. The following puzzle (taken from Dinesman 1968) is typical of a large class of simple logic puzzles:
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?
In [78]:
(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 'baker: baker)
(list 'cooper: cooper)
(list 'fletcher: fletcher)
(list 'miller: miller)
(list 'smith: smith)))))))))
In [79]:
(define distinct?
(lambda (nums)
(or (null? nums)
(null? (cdr nums))
(and (not (member (car nums) (cdr nums)))
(distinct? (cdr nums))))))
In [80]:
(floors2)
Out[80]:
In [81]:
(cond
(#f 1)
(else 2))
Out[81]:
In [82]:
(cons 1 2)
Out[82]:
In [83]:
(contains (dict '((a 1) (b 2))) 'b)
Out[83]:
In [84]:
(current-directory)
Out[84]:
In [7]:
(cd "/tmp")
Out[7]:
In [85]:
(current-environment)
Out[85]:
In [86]:
(current-time)
Out[86]:
In [87]:
(cut 1 2 3)
Out[87]:
In [88]:
(letrec ((loop (lambda (n)
(if (= n 0)
(set! var (cut 23))
(loop (- n 1)))))
(var 0))
(loop 10)
var)
Out[88]:
In [89]:
(define my-odd? 'undefined)
(define my-even? 'undefined)
(letrec
((odd (lambda (n) (if (= n 0) #f (even (- n 1)))))
(even (lambda (n) (if (= n 0) #t (odd (- n 1))))))
(set! my-odd? odd)
(set! my-even? even))
In [90]:
(my-odd? 42)
Out[90]:
In [91]:
(my-even? 42)
Out[91]:
In [92]:
(my-odd? 43)
Out[92]:
In [93]:
(my-even? 43)
Out[93]:
In [94]:
(define x 1)
x
Out[94]:
In [1]:
(define (f a b) (+ a b)) ;; MIT-style (hidden lambda)
(f 5 6)
Out[1]:
In [2]:
(define f
(lambda (a b)
(+ a b)))
(f 5 6)
Out[2]:
In [95]:
(begin
(define y 2)
(print y)
)
(print y)
In [6]:
(define! myvar 42)
In [7]:
myvar
Out[7]:
In [8]:
(python-eval "myvar")
Out[8]:
In [96]:
(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?)))
In [97]:
(var-exp 'a)
Out[97]:
In [98]:
(lambda-exp 'a (var-exp 'a))
Out[98]:
In [99]:
(app-exp (lambda-exp 'a (var-exp 'a)) (var-exp 'a))
Out[99]:
In [100]:
(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)))))
In [101]:
(un-parse (var-exp 'a))
Out[101]:
In [102]:
(un-parse (lambda-exp 'a (var-exp 'a)))
Out[102]:
In [103]:
(un-parse (app-exp (lambda-exp 'a (var-exp 'a)) (var-exp 'a)))
Out[103]:
In [104]:
(define-syntax time
[(time ?exp) (let ((start (current-time)))
?exp
(- (current-time) start))])
In [105]:
(time (car '(1 2 3 4)))
Out[105]:
In [106]:
;;---------------------------------------------------------------------
;; collect is like list comprehension in Python
(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)])
(define filter-map
(lambda (f pred? values)
(if (null? values)
'()
(if (pred? (car values))
(cons (f (car values)) (filter-map f pred? (cdr values)))
(filter-map f pred? (cdr values))))))
In [107]:
(collect (* n n) for n in (range 10))
Out[107]:
In [108]:
(collect (* n n) for n in (range 5 20 3))
Out[108]:
In [109]:
(collect (* n n) for n in (range 10) if (> n 5))
Out[109]:
In [110]:
;;---------------------------------------------------------------------
;; for loops
(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))])
(define for-repeat
(lambda (n f)
(if (< n 1)
'done
(begin
(f)
(for-repeat (- n 1) f)))))
(define for-iterate1
(lambda (values f)
(if (null? values)
'done
(begin
(f (car values))
(for-iterate1 (cdr values) f)))))
(define for-iterate2
(lambda (i values f)
(if (null? values)
'done
(begin
(f (car values) i)
(for-iterate2 (+ i 1) (cdr values) f)))))
In [111]:
(define matrix2d
'((10 20)
(30 40)
(50 60)
(70 80)))
(define matrix3d
'(((10 20 30) (40 50 60))
((70 80 90) (100 110 120))
((130 140 150) (160 170 180))
((190 200 210) (220 230 240))))
In [112]:
(begin
(define hello 0)
(for 5 times do (set! hello (+ hello 1)))
hello
)
Out[112]:
In [113]:
(for sym in '(a b c d) do (define x 1) (set! x sym) x)
Out[113]:
In [114]:
(for n in (range 10 20 2) do n)
Out[114]:
In [115]:
(for n at (i j) in matrix2d do (list n 'coords: i j))
Out[115]:
In [116]:
(for n at (i j k) in matrix3d do (list n 'coords: i j k))
Out[116]:
In [117]:
(define-syntax scons
[(scons ?x ?y) (cons ?x (lambda () ?y))])
(define scar car)
(define scdr
(lambda (s)
(let ((result ((cdr s))))
(set-cdr! s (lambda () result))
result)))
(define first
(lambda (n s)
(if (= n 0)
'()
(cons (scar s) (first (- n 1) (scdr s))))))
(define nth
(lambda (n s)
(if (= n 0)
(scar s)
(nth (- n 1) (scdr s)))))
(define smap
(lambda (f s)
(scons (f (scar s)) (smap f (scdr s)))))
(define ones (scons 1 ones))
(define nats (scons 0 (combine nats + ones)))
(define combine
(lambda (s1 op s2)
(scons (op (scar s1) (scar s2)) (combine (scdr s1) op (scdr s2)))))
(define fibs (scons 1 (scons 1 (combine fibs + (scdr fibs)))))
(define facts (scons 1 (combine facts * (scdr nats))))
(define ! (lambda (n) (nth n facts)))
In [118]:
(! 5)
Out[118]:
In [119]:
(nth 10 facts)
Out[119]:
In [120]:
(nth 20 fibs)
Out[120]:
In [121]:
(first 30 fibs)
Out[121]:
In [122]:
(dict '((a 1)(b 2)))
Out[122]:
In [1]:
(dict '((a : 1)(b : 2)))
Out[1]:
In [123]:
(dir)
Out[123]:
In [124]:
(dir complex)
Out[124]:
In [125]:
(display "hello")
In [126]:
(div 5 3)
Out[126]:
In [127]:
(eq? '(1 2) '(1 2))
Out[127]:
In [128]:
(equal? '(1 2) '(1 2))
Out[128]:
In [129]:
(eqv? '(1 2) '(1 2))
Out[129]:
In [130]:
(error "procedure-name" "What is ~a" 'huh?)
In [131]:
(eval '(+ 1 1))
Out[131]:
In [132]:
(eval-ast (parse-string "(+ 1 2)"))
Out[132]:
In [133]:
(even? 12121)
Out[133]:
In [134]:
(exit)
Out[134]:
In [135]:
(float 34)
Out[135]:
In [136]:
(for-each (lambda (n) (print n)) '(3 4 5))
In [137]:
(format "This uses formatting ~a ~s ~%" 'apple 'apple)
Out[137]:
In [9]:
(func (lambda (n) n))
Out[9]:
In [138]:
(load-as "test_all.ss" 'test)
In [139]:
((get 'test 'verify) "test-name" 1 = 1)
In [140]:
((get 'test '!) 5)
Out[140]:
In [141]:
(get-stack-trace)
Out[141]:
In [1]:
(getitem #(1 2 3) 0)
Out[1]:
In [2]:
(getitem (dict '((a 100))) 'a)
Out[2]:
In [3]:
(getitem (globals) "keys")
Out[3]:
In [12]:
(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)])
(define filter-map
(lambda (f pred? values)
(if (null? values)
'()
(if (pred? (car values))
(cons (f (car values)) (filter-map f pred? (cdr values)))
(filter-map f pred? (cdr values))))))
(collect x for x in (map (lambda (n) n) ((getitem (globals) "keys"))) if (string<? x "b"))
Out[12]:
In [145]:
(try
(import "math")
(catch e e
(import "Graphics")))
Out[145]:
In [146]:
(math.cos 0.6)
Out[146]:
In [147]:
(import-as "math" 'm)
Out[147]:
In [148]:
(m.sin 0.6)
Out[148]:
In [149]:
(import-from "math" "sin" "cos")
Out[149]:
In [150]:
(sin .5)
Out[150]:
In [151]:
(int 4.5)
Out[151]:
In [152]:
(integer->char 78)
Out[152]:
In [153]:
(iter? 3)
Out[153]:
In [154]:
(define g (globals))
(iter? (g.keys))
Out[154]:
In [13]:
(lambda (n) n) ;; identity function
Out[13]:
In [14]:
((lambda (i) i) 89)
Out[14]:
In [155]:
((lambda x x) 1 2 3 4 5)
Out[155]:
In [156]:
((lambda (x . y) (list x y)) 1 2 3 4 5)
Out[156]:
In [157]:
((lambda (a b . z) (list a b z)) 1 2 3 4 5)
Out[157]:
In [158]:
((lambda (a b . z) (list a b z)) 1 2 3)
Out[158]:
In [159]:
((lambda (a b . z) (list a b z)) 1 2)
Out[159]:
In [160]:
(try ((lambda (a b . z) (list a b z)) 1)
(catch e e "not enough arguments given"))
Out[160]:
In [161]:
(length '(1 2 3 4))
Out[161]:
In [162]:
(let ((v (vector 1 2 3)))
(vector-set! v 2 'a)
v)
Out[162]:
In [163]:
(try (let loop ((n 5))
n
(if (= n 0)
(raise 'blastoff!))
(loop (- n 1)))
(catch e e))
Out[163]:
In [164]:
(let* ((a 1) (b a))
b)
Out[164]:
In [165]:
(letrec
((fact (lambda (n)
(if (= n 1)
1
(* (fact (- n 1)) n)))))
(fact 5))
Out[165]:
In [166]:
(list 1 2 3 4)
Out[166]:
In [167]:
(list->string '(#\1 #\2))
Out[167]:
In [168]:
(list->vector '(1 2 3))
Out[168]:
In [169]:
(list-ref '(1 2 3) 0)
Out[169]:
In [170]:
(list? 123)
Out[170]:
In [171]:
(load "sllgen.ss")
In [172]:
(load-as "sllgen.ss" 'sll)
In [173]:
sll
Out[173]:
In [174]:
(make-set '(1 2 3 1 2 3))
Out[174]:
In [175]:
(make-vector 8)
Out[175]:
In [176]:
(map (lambda (n) (+ n 2)) '(3 4 5))
Out[176]:
In [177]:
(map (lambda (n) (print n)) '(3 4 5))
Out[177]:
In [178]:
(max (range 10))
Out[178]:
In [179]:
(member 10 (range 10))
Out[179]:
In [180]:
(member 0 (range 10))
Out[180]:
In [181]:
(member "b" '("a" "b" "c"))
Out[181]:
In [182]:
(memq 'b '(a b c))
Out[182]:
In [6]:
(memq 2 '(1.0 2.0 3.0))
Out[6]:
In [5]:
(memv 2 '(1.0 2.0 3.0))
Out[5]:
In [184]:
(min (range 10))
Out[184]:
In [185]:
(mod 7 3)
Out[185]:
In [186]:
(modulo 7 3)
Out[186]:
In [187]:
(newline)
In [188]:
(not #f)
Out[188]:
In [189]:
(not #t)
Out[189]:
In [190]:
(null? '())
Out[190]:
In [191]:
(null? '(1))
Out[191]:
In [192]:
(number->string 76.23)
Out[192]:
In [193]:
(number? 7623.3)
Out[193]:
In [194]:
(odd? 65)
Out[194]:
In [195]:
(or #t (/ 8 0))
Out[195]:
In [196]:
(pair? '())
Out[196]:
In [15]:
(pair? (cons 1 2))
Out[15]:
In [197]:
(parse '(+ 2 3))
Out[197]:
In [198]:
(parse-string "(+ 1 2)")
Out[198]:
In [199]:
(print "this string")
In [200]:
(printf "many possible ~a ~s" 'things "and etc")
In [201]:
(procedure? procedure?)
Out[201]:
In [202]:
(python-eval "1 + 4")
Out[202]:
In [203]:
(python-exec
"
x = 1
print(x)
")
In [204]:
`(list ,(+ 1 2) 4)
Out[204]:
In [205]:
(quote (1 2 3))
Out[205]:
In [206]:
'(1 2 3)
Out[206]:
In [207]:
'#(1 2 3)
Out[207]:
In [208]:
`#(1 ,(+ 2 4))
Out[208]:
In [209]:
(quotient 7 3)
Out[209]:
In [210]:
(rac '(1 2 3))
Out[210]:
In [211]:
(range 10)
Out[211]:
In [212]:
(rational 3 4)
Out[212]:
In [213]:
(rdc '(1 2 3))
Out[213]:
In [214]:
(read-string '(1 2 3))
Out[214]:
In [215]:
(record-case (cons 'banana (cons 'orange (cons (* 2 3) '())))
(apple (a b c) (list c b a r))
((cherry banana) (a . b) (list b a 5))
((orange) () 'no)
(else 2 3 4))
Out[215]:
In [216]:
(remainder 6 3)
Out[216]:
In [217]:
(reset-toplevel-env)
In [218]:
(reverse '(1 2 3))
Out[218]:
In [219]:
(round 34.7)
Out[219]:
In [6]:
(define x 12)
(set! x 13)
In [220]:
(define a '(1 2 3))
(set-car! a 7)
a
Out[220]:
In [221]:
(define a '(1 2 3))
(set-cdr! a '(4 5))
a
Out[221]:
In [2]:
(import "conx")
Out[2]:
In [3]:
(setitem conx "x" 42)
Out[3]:
In [4]:
(getitem conx "x")
Out[4]:
In [5]:
conx.x
Out[5]:
In [222]:
(define d (dict '((a 1)(b 2))))
(setitem d 'b 6)
d
Out[222]:
In [223]:
(snoc '0 '(1 2 3))
Out[223]:
In [224]:
(sort (lambda (a b) (< a b)) '(9 6 2 3 1 2 0))
Out[224]:
In [225]:
(sort < '(9 6 2 3 1 2 0))
Out[225]:
In [226]:
(sqrt 9)
Out[226]:
In [227]:
(string #\tab)
Out[227]:
In [228]:
(string->list "hello world!")
Out[228]:
In [229]:
(string->number "23.3")
Out[229]:
In [230]:
(string->symbol "apple")
Out[230]:
In [231]:
(string-append "abc" "123")
Out[231]:
In [232]:
(string-length "how long?")
Out[232]:
In [233]:
(string-ref "0123" 1)
Out[233]:
In [234]:
(string-split "1234" #\2)
Out[234]:
In [235]:
(string<? "apple" "zebra")
Out[235]:
In [236]:
(string=? "apple" "apple")
Out[236]:
In [237]:
(string? 'apple)
Out[237]:
In [238]:
(substring "01234" 1 3)
Out[238]:
In [239]:
(symbol "apple")
Out[239]:
In [240]:
(symbol->string 'apple)
Out[240]:
In [241]:
(symbol? "apple")
Out[241]:
In [28]:
(try
(/ 1 0)
(catch e e
(printf "Can't do that!")))
In [29]:
(try 3)
Out[29]:
In [30]:
(try 3
(finally (define did-it #t)))
Out[30]:
In [31]:
did-it
Out[31]:
In [32]:
(try (/ 3 0)
(catch e e)
(finally (print 'yes 4)))
Out[32]:
In [33]:
(try (raise 'yes)
(catch e e))
Out[33]:
In [34]:
(try (try (raise 'yes))
(catch e e))
Out[34]:
In [35]:
(try (try (begin 'one (raise 'oops) 'two))
(catch e e))
Out[35]:
In [36]:
(* 10 (try (begin 'one (raise 'oops) 'two)
(catch ex 3 4)))
Out[36]:
In [37]:
(* 10 (try (begin 'one 'two 5)
(catch ex 3 4)))
Out[37]:
In [38]:
(* 10 (try (begin 'one (raise 'oops) 5)
(catch ex (list 'ex: ex) 4)))
Out[38]:
In [39]:
(try (* 10 (try (begin 'one (raise 'oops) 5)
(catch ex (list 'ex: ex) (raise ex) 4)))
(catch e e))
Out[39]:
In [40]:
(try (* 10 (try (begin 'one (raise 'oops) 5)
(catch ex (list 'ex: ex) (raise ex) 4)
(finally (print 'two 7))))
(catch e e))
Out[40]:
In [41]:
(try (* 10 (try (begin 'one (raise 'oops) 5)
(catch ex (list 'ex: ex) (raise 'bar) 4)))
(catch x 'hello 77))
Out[41]:
In [42]:
(try 3
(finally (print 'hi 4)))
Out[42]:
In [43]:
(try (div 10 0)
(catch e (cadr e)))
Out[43]:
In [44]:
(try (let ((x
(try (div 10 0)))) x)
(catch e (cadr e)))
Out[44]:
In [45]:
(let ((x (try (div 10 2)
(catch e -1)))) x)
Out[45]:
In [46]:
(let ((x
(try (div 10 0)
(catch e -1)))) x)
Out[46]:
In [47]:
(let ((x (try (div 10 2)
(catch e -1)
(finally (print 'closing-files 42))))) x)
Out[47]:
In [48]:
(let ((x (try (div 10 0)
(catch e -1)
(finally (print 'closing-files 42))))) x)
Out[48]:
In [49]:
(let ((x (try (div 10 2)
(finally (print 'closing-files 42))))) x)
Out[49]:
In [50]:
(try (let ((x (try (div 10 0)
(catch e -1 (raise 'foo))
(finally (print 'closing-files 42))))) x) (catch e e))
Out[50]:
In [51]:
(try (let ((x (try (div 10 0)
(catch e -1 (raise 'foo))
(finally (print 'closing-files (raise 'ack) 42)))))
x) (catch e e))
Out[51]:
In [52]:
(try (let ((x (try (div 10 0)
(catch e -1 (raise 'foo))
(finally (print 'closing-files (raise 'ack) 42)))))
x)
(catch e (if (equal? e 'ack) 99 (raise 'doug)))
(finally (print 'closing-outer-files)))
Out[52]:
In [53]:
(try (try (let ((x (try (div 10 0)
(catch e -1 (raise 'foo))
(finally 'closing-files (raise 'ack) 42))))
x)
(catch e (if (equal? e 'foo) 99 (raise 'doug)))
(finally (print 'closing-outer-files))) (catch e e))
Out[53]:
In [54]:
(typeof 23.4)
Out[54]:
In [267]:
(unparse (parse '(+ 1 2)))
Out[267]:
In [268]:
(unparse-procedure (lambda (x) (+ x 1)))
Out[268]:
In [269]:
(use-lexical-address #t)
In [270]:
(use-stack-trace #t)
In [271]:
(use-tracing)
Out[271]:
In [272]:
(use-tracing #t)
(define fact
(lambda (n)
(if (= n 1)
1
(* n (fact (- n 1))))))
(fact 1)
(use-tracing #f)
In [273]:
(vector 1 2 (+ 3 3))
Out[273]:
In [274]:
(vector->list #(1 2 3))
Out[274]:
In [275]:
(vector-length #(1 2 3))
Out[275]:
In [276]:
(vector-ref #(1 2 3) 2)
Out[276]:
In [277]:
(let ((v #(1 2 3)))
(vector-set! v 1 4)
v)
Out[277]:
In [278]:
(vector? '(1 2 3))
Out[278]:
In [279]:
(void)
In [280]:
(zero? 0.0)
Out[280]:
In [281]:
((lambda ((n : 1)) n))
Out[281]:
In [282]:
((lambda ((n : 2)) n))
Out[282]:
In [283]:
((lambda ((n : 1)) n) 3)
Out[283]:
In [284]:
((lambda (a b c) (list a b c)) 1 2 3)
Out[284]:
In [285]:
((lambda (a b c) (list a b c)) 1 2 (c : 3))
Out[285]:
In [286]:
((lambda (a b c) (list a b c)) 1 (b : 2) (c : 3))
Out[286]:
In [287]:
((lambda (a b c) (list a b c)) (a : 1) (b : 2) (c : 3))
Out[287]:
In [288]:
((lambda (a b c) (list a b c)) 1 (c : 3) (b : 2))
Out[288]:
In [289]:
((lambda ((n : 1)) n) (n : 3))
Out[289]:
The following items are currently missing from Calysto Scheme. However, much of their functionality can be achieved by using Python versions. For example you can use the Python bin
function to create a binary representation, since the Scheme binary
predicate has not been defined.