{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Addendum\n", "\n", "Additional Calysto Scheme commands and functionality. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a unique container for an item:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "#&a" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(box 'a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get the item from the box:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "a" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(unbox (box 'a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Is an item a box?" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "#t" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(box? (box 'a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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)." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "0.10972947659033694" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(random 1.0)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(random 2) ;; 0 or 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Truncate a float, or rational number by turning it into an integer:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(int 3.1415)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(int 3/2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sort a list using a comparison operator:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "(\"a\" \"b\" \"z\")" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(sort string a b)) '(\"z\" \"a\" \"b\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get the typing completions for an object:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "(% * + - / // < <= = > >= 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 stringstring 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? λ)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(get-completions)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "(__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)" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(get-completions dir)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Assert that (op exp1 exp2) is true:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "ok" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(assert = 1 1)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[0;31m\n", "Traceback (most recent call last):\n", " File \"In [15]\", line 1, col 1, in 'assert'\n", " File \"In [15]\", line 1, col 1\n", "AssertionError: \n", "\n", "\u001b[0m" ] } ], "source": [ "(assert = 1 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can also provide a message:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[0;31m\n", "Traceback (most recent call last):\n", " File \"In [16]\", line 1, col 1, in 'assert'\n", " File \"In [16]\", line 1, col 1\n", "AssertionError: message\n", "\n", "\u001b[0m" ] } ], "source": [ "(assert = 1 2 \"message\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Clear all of the unit tests define so far:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "tags": [] }, "outputs": [], "source": [ "(clear-unit-tests)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a new group of unit-tests:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "tags": [] }, "outputs": [], "source": [ "(define-tests my-name\n", " (assert = 1 1 \"name 1\")\n", " (assert = 1 2 \"name 2\")\n", " (assert = 1 3 \"case 1\")\n", " (assert = 1 4 \"case 2\")\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run all, or groups of unit tests:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Testing group 'my-name'...\n", " Error: my-name \"name 2\" at line 3, column 3 of In [18]\n", " Error: my-name \"case 1\" at line 4, column 3 of In [18]\n", " Error: my-name \"case 2\" at line 5, column 3 of In [18]\n", "=================\n", "Testing completed!\n", " Time : 0.01 seconds\n", " Total tests defined: 4 \n", " Total tests tested : 4 \n", " Right: 1 \n", " Wrong: 3 \n" ] } ], "source": [ "(run-tests)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Testing group 'my-name'...\n", " Error: my-name \"name 2\" at line 3, column 3 of In [18]\n", " Error: my-name \"case 1\" at line 4, column 3 of In [18]\n", " Error: my-name \"case 2\" at line 5, column 3 of In [18]\n", "=================\n", "Testing completed!\n", " Time : 0.00 seconds\n", " Total tests defined: 4 \n", " Total tests tested : 4 \n", " Right: 1 \n", " Wrong: 3 \n" ] } ], "source": [ "(run-tests my-name)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Testing group 'my-name'...\n", " Error: my-name \"name 2\" at line 3, column 3 of In [18]\n", "\n", "Traceback (most recent call last):\n", " File \"In [18]\", line 3, col 3, in 'assert'\n", " File \"In [18]\", line 3, col 3\n", "AssertionError: name 2\n", "\n", " Procedure : =\n", " src : 1\n", " src eval: 1\n", " result : 2\n", "=================\n", "Testing completed!\n", " Time : 0.00 seconds\n", " Total tests defined: 4 \n", " Total tests tested : 2 \n", " Right: 1 \n", " Wrong: 1 \n" ] } ], "source": [ "(run-tests my-name \"name\")" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Testing group 'my-name'...\n", " Error: my-name \"case 1\" at line 4, column 3 of In [18]\n", "\n", "Traceback (most recent call last):\n", " File \"In [18]\", line 4, col 3, in 'assert'\n", " File \"In [18]\", line 4, col 3\n", "AssertionError: case 1\n", "\n", " Procedure : =\n", " src : 1\n", " src eval: 1\n", " result : 3\n", "=================\n", "Testing completed!\n", " Time : 0.01 seconds\n", " Total tests defined: 4 \n", " Total tests tested : 1 \n", " Right: 0 \n", " Wrong: 1 \n" ] } ], "source": [ "(run-tests my-name 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Change directory can expand \"home\":" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "\"/home/dblank\"" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(cd \"~/\") ;; expands ~ to home" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "See what host environment Calysto Scheme is running under (returns \"python\" or \"scheme\"):" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "\"python\"" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(host-environment)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a dictionary:" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "{'a': 1, 'b': 2}" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(define d (dict '((a : 1)(b : 2))))\n", "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Does the dictionary or vector contain the item?" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "#t" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(has-item? d 'a)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "#f" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(has-item? d 'c)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "tags": [] }, "outputs": [], "source": [ "(define v (vector 10 20 30))" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "#f" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(has-item? v 10)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#f" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(has-item? v 3)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "#t" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(has-item? v (slice 0 2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Python slice of a object:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "slice(None, 5, None)" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(slice None 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "New macros via functions:" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "tags": [] }, "outputs": [], "source": [ "(define-syntax time-it\n", " (lambda (sexp)\n", " `(let ((start (current-time))\n", " (result ,(cadr sexp)))\n", " (printf \"Time: ~s~%\" (- (current-time) start))\n", " result)))" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "((lambda (start result) (printf \"Time: ~s~%\" (- (current-time) start)) result) (current-time) 42)" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(unparse (parse '(time-it 42)))" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Time: 0.0004911422729492188\n" ] }, { "data": { "text/plain": [ "3" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(time-it (+ 1 2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Calysto Scheme 3", "language": "scheme", "name": "calysto_scheme" }, "language_info": { "codemirror_mode": { "name": "scheme" }, "mimetype": "text/x-scheme", "name": "scheme", "pygments_lexer": "scheme" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }