Additional Calysto Scheme commands and functionality.
Create a unique container for an item:
(box 'a)
#&a
Get the item from the box:
(unbox (box 'a))
a
Is an item a box?
(box? (box 'a))
#t
Generate random numbers. If given a floating-point number, then return a number between 0 and < number [0, number). If given an integer, return an integer between 0 and < number [0, number).
(random 1.0)
0.10972947659033694
(random 2) ;; 0 or 1
1
Truncate a float, or rational number by turning it into an integer:
(int 3.1415)
3
(int 3/2)
1
Sort a list using a comparison operator:
(sort string<? '("z" "a" "b"))
("a" "b" "z")
(sort (lambda (a b) (> a b)) '("z" "a" "b"))
("z" "b" "a")
Get the typing completions for an object:
(get-completions)
(% * + - / // < <= = > >= SCHEMEPATH abort abs and append apply assert assq assv atom? boolean? box box? caaaar caaadr caaar caadar caaddr caadr caar cadaar cadadr cadar caddar cadddr caddr cadr call-with-current-continuation call/cc car case cases cd cdaaar cdaadr cdaar cdadar cdaddr cdadr cdar cddaar cddadr cddar cdddar cddddr cdddr cddr cdr char->integer char->string char-alphabetic? char-numeric? char-whitespace? char=? char? clear-unit-tests cond cons current-directory current-environment current-time cut define-datatype dict dir display div eq? equal? eqv? error eval eval-ast even? exit expt float for-each format get-attr get-completions get-exception-message get-item get-stack-trace globals has-attr? has-item? host-environment import import-as import-from int integer->char iter? lambda length let let* letrec list list->string list->vector list-ref list? load load-as macros make-set make-vector map max member memq memv min mod modulo newline not null? number->string number? odd? or pair? parse parse-string print printf procedure? property python-eval python-exec quit quotient rac random range rational rdc read-string record-case remainder require reset-toplevel-env reverse round set-attr! set-car! set-cdr! set-item! snoc sort sqrt string string->list string->number string->symbol string-append string-join string-length string-ref string-split string<? string=? string? substring symbol->string symbol? trace-lambda typeof unbox unparse unparse-procedure use-lexical-address use-stack-trace use-tracing vector vector->list vector-length vector-ref vector-set! vector? void zero? λ)
(get-completions dir)
(__call__ __class__ __delattr__ __dict__ __dir__ __doc__ __eq__ __format__ __ge__ __getattribute__ __getitem__ __gt__ __hash__ __init__ __init_subclass__ __iter__ __le__ __len__ __lt__ __module__ __ne__ __new__ __next__ __reduce__ __reduce_ex__ __repr__ __setattr__ __sizeof__ __str__ __subclasshook__ __weakref__ car cdr next)
Assert that (op exp1 exp2) is true:
(assert = 1 1)
ok
(assert = 1 2)
Traceback (most recent call last):
File "In [15]", line 1, col 1, in 'assert'
File "In [15]", line 1, col 1
AssertionError:
Can also provide a message:
(assert = 1 2 "message")
Traceback (most recent call last):
File "In [16]", line 1, col 1, in 'assert'
File "In [16]", line 1, col 1
AssertionError: message
Clear all of the unit tests define so far:
(clear-unit-tests)
Define a new group of unit-tests:
(define-tests my-name
(assert = 1 1 "name 1")
(assert = 1 2 "name 2")
(assert = 1 3 "case 1")
(assert = 1 4 "case 2")
)
Run all, or groups of unit tests:
(run-tests)
Testing group 'my-name'... Error: my-name "name 2" at line 3, column 3 of In [18] Error: my-name "case 1" at line 4, column 3 of In [18] Error: my-name "case 2" at line 5, column 3 of In [18] ================= Testing completed! Time : 0.01 seconds Total tests defined: 4 Total tests tested : 4 Right: 1 Wrong: 3
(run-tests my-name)
Testing group 'my-name'... Error: my-name "name 2" at line 3, column 3 of In [18] Error: my-name "case 1" at line 4, column 3 of In [18] Error: my-name "case 2" at line 5, column 3 of In [18] ================= Testing completed! Time : 0.00 seconds Total tests defined: 4 Total tests tested : 4 Right: 1 Wrong: 3
(run-tests my-name "name")
Testing group 'my-name'... Error: my-name "name 2" at line 3, column 3 of In [18] Traceback (most recent call last): File "In [18]", line 3, col 3, in 'assert' File "In [18]", line 3, col 3 AssertionError: name 2 Procedure : = src : 1 src eval: 1 result : 2 ================= Testing completed! Time : 0.00 seconds Total tests defined: 4 Total tests tested : 2 Right: 1 Wrong: 1
(run-tests my-name 1)
Testing group 'my-name'... Error: my-name "case 1" at line 4, column 3 of In [18] Traceback (most recent call last): File "In [18]", line 4, col 3, in 'assert' File "In [18]", line 4, col 3 AssertionError: case 1 Procedure : = src : 1 src eval: 1 result : 3 ================= Testing completed! Time : 0.01 seconds Total tests defined: 4 Total tests tested : 1 Right: 0 Wrong: 1
Change directory can expand "home":
(cd "~/") ;; expands ~ to home
"/home/dblank"
See what host environment Calysto Scheme is running under (returns "python" or "scheme"):
(host-environment)
"python"
Define a dictionary:
(define d (dict '((a : 1)(b : 2))))
d
{'a': 1, 'b': 2}
Does the dictionary or vector contain the item?
(has-item? d 'a)
#t
(has-item? d 'c)
#f
(define v (vector 10 20 30))
(has-item? v 10)
#f
(has-item? v 3)
#f
(has-item? v (slice 0 2))
#t
The Python slice of a object:
(slice None 5)
slice(None, 5, None)
New macros via functions:
(define-syntax time-it
(lambda (sexp)
`(let ((start (current-time))
(result ,(cadr sexp)))
(printf "Time: ~s~%" (- (current-time) start))
result)))
(unparse (parse '(time-it 42)))
((lambda (start result) (printf "Time: ~s~%" (- (current-time) start)) result) (current-time) 42)
(time-it (+ 1 2))
Time: 0.0004911422729492188
3