{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"# Vspace MeTTa Language\$n",
"\n",
"[Vspace MeTTa](https://github.com/Vspace/vspace_scheme) is a real MeTTa 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.\n",
"\n",
"For more of an overview, see [Reference Guide for Vspace MeTTa](Reference%20Guide%20for%20Vspace%20MeTTa.ipynb)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Vspace MeTTa Macros\$n",
"\n",
"The following are implemented as macros and are syntactic sugar on simpler forms:\n",
"\n",
"* let\$n",
"* letrec\$n",
"* let*\n",
"* and\$n",
"* or\$n",
"* cond\$n",
"* case\$n",
"* cases\$n",
"* record-case\$n",
"* define-datatype\$n",
"* define-tests\$n",
"* assert\$n",
"* run-tests"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Vspace MeTTa Procedures"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## % (modulo)\n",
"(% arg0 arg1): modulo procedure for two arguments (aliases mod and modulo)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(% 5 2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## * (multiply)\n",
"(* ...): multiplication procedure; multiplies all arguments"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"120"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(* 2 3 4 5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## + (addition)\n",
"(+ ...): addition procedure; adds all arguments"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(+ 1 1 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## - (subtraction)\n",
"(- ...): subtraction procedure; subtracts all arguments"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"-1"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(- 5 4 2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## / (real division)\n",
"(/ ...): division procedure; divides all arguments"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"3/2"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(/ 3 2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## // (integer division)\n",
"(// arg0 arg1): quotient procedure for rationals/ints; divides arg0 by arg1 (aliases div and quotient)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(// 3 2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## < (less than)\n",
"(< arg0 arg1): less-than procedure for two arguments"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#t"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(< 6 7)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## <= (less than or equals to)\n",
"(<= arg0 arg1): less-than or equal procedure for two arguments"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#f"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(<= 5.5 5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## = (numeric equals)\n",
"(= arg0 arg1): numeric equality procedure for two arguments"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#f"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(= 7 8)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## > (greater than)\n",
"(> arg0 arg1): greater-than procedure for two arguments"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#t"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(> 8 6)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## >= (greater than or equals to)\n",
"(>= arg0 arg1): greater-than or equal procedure for two arguments"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#t"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(>= 9 9)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## abort\$n",
"(abort) : aborts processing and returns to top level"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(abort)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## abs\$n",
"(abs value): absolute value procedure"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"67"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(abs -67)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## and\$n",
"(and ITEM ...)\n",
"\n",
"`and` is defined via a macro (define-syntax)."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#f"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(and #f (/ 6 0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## append\$n",
"(append ...): append lists together into a single list"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(1 2 3 4 5)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(append (1 2 3) (4 5))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## apply\$n",
"(apply PROCEDURE (args...)): apply the PROCEDURE to the args"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(apply + (1 2 3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## assq\$n",
"(assv KEY ((ITEM VALUE) ...)): look for KEY in ITEMs; return matching (ITEM VALUE) or #f if not found. Uses eq? to look for key. "
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(key 2)"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(assq 'key ((apple 1)(key 2)))"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#f"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(assq 'key ((apple 1)(banana 2)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## assv\$n",
"(assv KEY ((ITEM VALUE) ...)): look for KEY in ITEMs; return matching (ITEM VALUE) or #f if not found. Uses eqv? to look for key. "
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(key 2)"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(assv 'key ((apple 1)(key 2)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## atom?\n",
"(atom? ITEM): return #t if ITEM is a atom, #f otherwise"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#f"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(atom? (cons 1 2))"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#t"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(atom? 'a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## boolean?\n",
"(boolean? ITEM): return #t if ITEM is a boolean value"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#t"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(boolean? #t)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## caaaar\$n",
"caaaar LIST): return the caaaar of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(hello there)"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(caaaar (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## caaadr\$n",
"(caaadr LIST): return the caaadr of the LIST "
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"in"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(caaadr (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## caaar\$n",
"(caaar LIST): return the caaar of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"((hello there) this is a test)"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(caaar (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## caadar\$n",
"(caadar LIST): return the caadar of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"((1 2 3) 4 5 6)"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(caadar (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## caaddr\$n",
"(caaddr LIST): return the caaddr of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"the"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(caaddr (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## caadr\$n",
"(caadr LIST): return the caadr of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(in)"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(caadr (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## caar\$n",
"(caar LIST): return the caar of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(((hello there) this is a test) what is this)"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(caar (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cadaar\$n",
"(cadaar LIST): return the cadaar of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"what"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cadaar (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cadadr\$n",
"(cadadr LIST): return the cadadr of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"another"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cadadr (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in) another) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cadar\$n",
"(cadar LIST): return the cadar of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(((1 2 3) 4 5 6) 7 8 9)"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cadar (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## caddar\$n",
"(caddar LIST): return the caddar of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"another"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(caddar (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cadddr\$n",
"(cadddr LIST): return the cadddr of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cadddr (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## caddr\$n",
"(caddr LIST): return the (car (cdr (cdr LIST)))"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(the)"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(caddr (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cadr\$n",
"(cadr LIST): return the (car (cdr LIST))"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"((in))"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cadr (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## call-with-current-continuation\$n",
"(call-with-current-continuation PROCEDURE): \n",
"\n",
"See call/cc for examples."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## call/cc\$n",
"(call/cc PROCEDURE): call-with-current-continuation "
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"40"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(* 10 (call/cc $k) 4)))"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"40"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(* 10 (call/cc $k) (+ 1 (k 4)))))"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"50"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(* 10 (call/cc $k) \n",
" (+ 1 (call/cc \n",
" $j) \n",
" (+ 2 (j (k 5)))))))))"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"60"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(* 10 (call/cc $k) \n",
" (+ 1 (call/cc \n",
" $j) \n",
" (+ 2 (k (j 5)))))))))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## car\$n",
"(car LIST) returns the first element of LIST"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"((((hello there) this is a test) what is this) (((1 2 3) 4 5 6) 7 8 9) another item)"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(car (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## case\$n",
"(case ...)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(case 'thing1 (thing2 1) (thing1 2))"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(case 'thing1 (thing2 1) ((thing1 thing3) 2))"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(case 'thingx (thing2 1) ((thing1 thing3) 2) (%void% 3))"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(case 'banana\$n",
" (apple 'no)\n",
" ((cherry banana) 1 2 3)\n",
" (%void% 'no))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cases\$n",
"(cases...)\n",
"\n",
"See examples in define-datatype."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cd\$n",
"(cd [PATH]): get the current directory, or set it if PATH is given (alias current-directory)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"\"/\""
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cd \"/\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cdaaar\$n",
"(cdaaar LIST): return the cdaaar of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(this is a test)"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cdaaar (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cdaadr\$n",
"(cdaadr LIST): return the cdaadr of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cdaadr (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cdaar\$n",
"(cdaar LIST): return the cdaar of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(what is this)"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cdaar (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cdadar\$n",
"(cdadar LIST): return the cdadar of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(7 8 9)"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cdadar (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cdaddr\$n",
"(cdaddr LIST): return the cdaddr of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cdaddr (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cdadr\$n",
"(cdadr LIST): return the cdadr of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cdadr (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cdar\$n",
"(cdar LIST): return the cdar of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"((((1 2 3) 4 5 6) 7 8 9) another item)"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cdar (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cddaar\$n",
"(cddaar LIST): return the cddaar of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(is this)"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cddaar (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cddadr\$n",
"(cddadr LIST): return the cddadr of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(other)"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cddadr (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in) some other) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cddar\$n",
"(cddar LIST): return the cddar of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(another item)"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cddar (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cdddar\$n",
"(cdddar LIST): return the cdddar of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(item)"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cdddar (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cddddr\$n",
"(cddddr LIST): return the cddddr of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cddddr (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cdddr\$n",
"(cdddr LIST): return the cddr of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(list)"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cdddr (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cddr\$n",
"(cddr LIST): return the cddr of the LIST"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"((the) list)"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cddr (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cdr\$n",
"(cdr LIST) returns rest of LIST after (car LIST)"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(((in)) (the) list)"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cdr (((((hello there) this is a test) what is this) \n",
" (((1 2 3) 4 5 6) 7 8 9) another item) ((in)) (the) list))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## char->integer\$n",
"(char->integer CHAR): return associated number of CHAR "
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"49"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(char->integer #\\1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## char->string\$n",
"(char->string CHAR): return the character as a string"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"\"a\""
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(char->string #\\a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## char-alphabetic?\n",
"(char-alphabetic? CHAR): return #t if CHAR is an alphabetic character, #f otherwise"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#f"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(char-alphabetic? #\\1)"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#t"
]
},
"execution_count": 65,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(char-alphabetic? #\\a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## char-numeric?\n",
"(char-numeric? CHAR): return #t if CHAR is a whitespace character, #f otherwise"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#t"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(char-numeric? #\\2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## char-whitespace?\n",
"(char-whitespace? CHAR): return #t if CHAR is a whitespace character, #f otherwise"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#t"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(char-whitespace? #\\tab)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## char=?\n",
"(char=? CHAR1 CHAR2): return #t if CHAR1 has the same values as CHAR2, #f otherwise"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#f"
]
},
"execution_count": 68,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(char=? #\\1 #\\2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## char?\n",
"(char? ITEM): return #t if ITEM is a character, #f otherwise"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#f"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(char? \"h\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## choose\$n",
"\n",
"This is the main interface to the non-deterministic interface. Using (choose options) combined with (require BOOLEAN-TEST) you can go back and select another option.\n",
"\n",
"(choose OPTIONS): pick one, with ability to go back and pick another later"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(= (x (choose 1 2 3))"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(require #f)"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(require #f)"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"\"no more choices\""
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(require #f)"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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:\n",
"\n",
"> 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?"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(= (floors2\$n",
" $)\n",
" (let ((baker (choose 1 2 3 4 5)))\n",
" (require (not (= baker 5)))\n",
" (let ((fletcher (choose 1 2 3 4 5)))\n",
" (require (not (= fletcher 5)))\n",
" (require (not (= fletcher 1)))\n",
" (let ((cooper (choose 1 2 3 4 5)))\n",
" (require (not (= cooper 1)))\n",
" (require (not (= (abs (- fletcher cooper)) 1)))\n",
" (let ((smith (choose 1 2 3 4 5)))\n",
" (require (not (= (abs (- smith fletcher)) 1)))\n",
" (let ((miller (choose 1 2 3 4 5)))\n",
" (require (> miller cooper))\n",
" (require (distinct? (list baker cooper fletcher miller smith)))\n",
" (list\$n",
" (list 'baker: baker)\n",
" (list 'cooper: cooper)\n",
" (list 'fletcher: fletcher)\n",
" (list 'miller: miller)\n",
" (list 'smith: smith)))))))))"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(= (distinct?\n",
" $nums)\n",
" (or (null? nums)\n",
" (null? (cdr nums))\n",
" (and (not (member (car nums) (cdr nums)))\n",
" (distinct? (cdr nums))))))"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"((baker: 3) (cooper: 2) (fletcher: 4) (miller: 5) (smith: 1))"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(floors2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cond\$n",
"(case CONDITIONS): CONDITIONS are composed of (TEST RETURN). cond will return the first RETURN where TEST is #t. `else` is considered to be #t."
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(case \n",
" (#f 1) \n",
" (%void% 2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cons\$n",
"(cons ITEM1 ITEM2): return a list with ITEM1 as car and ITEM2 as cdr (ITEM2 is typically a list)"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(1 . 2)"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cons 1 2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## current-directory\$n",
"(current-directory [PATH]): get the current directory, or set it if PATH is given (alias cd)"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"\"/\""
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(current-directory)"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"\"/tmp\""
]
},
"execution_count": 84,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cd \"/tmp\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## current-environment\$n",
"(current-environment): returns the current environment"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(current-environment)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## current-time\$n",
"(current-time): returns the current time as number of seconds since 1970-1-1"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"1684219708.360304"
]
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(current-time)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## cut\$n",
"(cut ARGS...): return to toplevel with ARGS"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(1 2 3)"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(cut 1 2 3)"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(23)"
]
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(letrec ((loop $n) \n",
" (if (= $n 0) \n",
" (set! var (cut 23)) \n",
" (loop (- $n 1)))))\n",
" (var 0))\n",
" (loop 10)\n",
" var)"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(= (my-odd? 'undefined)\n",
"(= (my-even? 'undefined)\n",
"\n",
"(letrec\$n",
" ((odd $n) (if (= $n 0) #f (even (- $n 1)))))\n",
" (even $n) (if (= $n 0) #t (odd (- $n 1))))))\n",
" (set! my-odd? odd)\n",
" (set! my-even? even))"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#f"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(my-odd? 42)"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#t"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(my-even? 42)"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#t"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(my-odd? 43)"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"#f"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(my-even? 43)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## define\$n",
"(= (SYMBOL VALUE): bind a symbol to a value in the top-level-environment \n",
"(= ((SYMBOL ARGS) VALUE): the MIT-define"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(= (x 1)\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(= ((f a b) (+ a b)) ;; MIT-style (hidden lambda)\n",
"(f 5 6)"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(= (f\$n",
" $a b)\n",
" (+ a b)))\n",
"(f 5 6)"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\$n",
"2\$n"
]
}
],
"source": [
"(begin\$n",
" (= (y 2)\n",
" (print y)\n",
")\n",
"(print y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## define! \n",
"\n",
"Define an item in the shared environment with Python.\n",
"\n",
"(define! NAME VALUE)"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(define! myvar 42)"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"42"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"myvar"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"42"
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(python-eval \"myvar\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## define-datatype\$n",
"(define-datatype TYPE TYPE-TEST-NAME COMPONENTS): define a new datatype. Creates the constructors and tests for the datatype and components. For use with `cases`. See below for example. "
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(define-datatype lc-exp lc-exp?\n",
" (var-exp \n",
" (var symbol?))\n",
" (lambda-exp \n",
" (bound-var symbol?)\n",
" (body lc-exp?))\n",
" (app-exp\$n",
" (rator lc-exp?)\n",
" (rand lc-exp?)))"
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(var-exp a)"
]
},
"execution_count": 102,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(var-exp 'a)"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(lambda-exp a (var-exp a))"
]
},
"execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(lambda-exp 'a (var-exp 'a))"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(app-exp (lambda-exp a (var-exp a)) (var-exp a))"
]
},
"execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(app-exp (lambda-exp 'a (var-exp 'a)) (var-exp 'a))"
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(= (un-parse\$n",
" $exp)\n",
" (cases lc-exp exp\$n",
" (var-exp (var) var)\n",
" (lambda-exp (bound-var body) (list bound-var body))\n",
" (app-exp (rator rand) (list rator rand)))))\n"
]
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"a"
]
},
"execution_count": 106,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(un-parse (var-exp 'a))"
]
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(a (var-exp a))"
]
},
"execution_count": 107,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(un-parse (lambda-exp 'a (var-exp 'a)))"
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"((lambda-exp a (var-exp a)) (var-exp a))"
]
},
"execution_count": 108,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(un-parse (app-exp (lambda-exp 'a (var-exp 'a)) (var-exp 'a)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## define-syntax\$n",
"(define-syntax NAME RULES): a method for creating macros"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(define-syntax time \n",
" [(time ?exp) (let ((start (current-time)))\n",
" ?exp\$n",
" (- (current-time) start))])"
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"0.00019049644470214844"
]
},
"execution_count": 110,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(time (car (1 2 3 4)))"
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
";;---------------------------------------------------------------------\n",
";; collect is like list comprehension in Python\$n",
"\n",
"(define-syntax collect\$n",
" [(collect ?exp for ?var in ?list)\n",
" (filter-map $?var) ?exp) $?var) #t) ?list)]\n",
" [(collect ?exp for ?var in ?list if ?condition)\n",
" (filter-map $?var) ?exp) $?var) ?condition) ?list)])\n",
"\n",
"(= (filter-map\$n",
" $f pred? values)\n",
" (if (null? values)\n",
" ()\n",
" (if (pred? (car values))\n",
" (cons (f (car values)) (filter-map f pred? (cdr values)))\n",
" (filter-map f pred? (cdr values))))))"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(0 1 4 9 16 25 36 49 64 81)"
]
},
"execution_count": 112,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(collect (* $n $n) for $n in (range 10))"
]
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(25 64 121 196 289)"
]
},
"execution_count": 113,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(collect (* $n $n) for $n in (range 5 20 3))"
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(36 49 64 81)"
]
},
"execution_count": 114,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(collect (* $n $n) for $n in (range 10) if (> $n 5))"
]
},
{
"cell_type": "code",
"execution_count": 115,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
";;---------------------------------------------------------------------\n",
";; for loops\$n",
"\n",
"(define-syntax for\$n",
" [(for ?exp times do . ?bodies)\n",
" (for-repeat ?exp $) . ?bodies))]\n",
" [(for ?var in ?exp do . ?bodies)\n",
" (for-iterate1 ?exp $?var) . ?bodies))]\n",
" [(for ?var at (?i) in ?exp do . ?bodies)\n",
" (for-iterate2 0 ?exp $?var ?i) . ?bodies))]\n",
" [(for ?var at (?i ?j . ?rest) in ?exp do . ?bodies)\n",
" (for ?var at (?i) in ?exp do\$n",
" (for ?var at (?j . ?rest) in ?var do . ?bodies))])\n",
"\n",
"(= (for-repeat\$n",
" $n f)\n",
" (if (< $n 1)\n",
" 'done\$n",
" (begin\$n",
" (f)\n",
" (for-repeat (- $n 1) f)))))\n",
"\n",
"(= (for-iterate1\$n",
" $values f)\n",
" (if (null? values)\n",
" 'done\$n",
" (begin\$n",
" (f (car values))\n",
" (for-iterate1 (cdr values) f)))))\n",
"\n",
"(= (for-iterate2\$n",
" $i values f)\n",
" (if (null? values)\n",
" 'done\$n",
" (begin\$n",
" (f (car values) i)\n",
" (for-iterate2 (+ i 1) (cdr values) f)))))"
]
},
{
"cell_type": "code",
"execution_count": 116,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(= (matrix2d\$n",
" ((10 20)\n",
" (30 40)\n",
" (50 60)\n",
" (70 80)))\n",
"\n",
"(= (matrix3d\$n",
" (((10 20 30) (40 50 60))\n",
" ((70 80 90) (100 110 120))\n",
" ((130 140 150) (160 170 180))\n",
" ((190 200 210) (220 230 240))))"
]
},
{
"cell_type": "code",
"execution_count": 117,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 117,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(begin \n",
" (= (hello 0)\n",
" (for 5 times do (set! hello (+ hello 1)))\n",
" hello\$n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 118,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"done"
]
},
"execution_count": 118,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(for sym in (a b c d) do (= (x 1) (set! x sym) x)"
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"done"
]
},
"execution_count": 119,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(for $n in (range 10 20 2) do $n)"
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"done"
]
},
"execution_count": 120,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(for $n at (i j) in matrix2d do (list $n 'coords: i j))"
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"done"
]
},
"execution_count": 121,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(for $n at (i j k) in matrix3d do (list $n 'coords: i j k))"
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(define-syntax scons\$n",
" [(scons ?x ?y) (cons ?x $) ?y))])\n",
"\n",
"(= (scar car)\n",
"\n",
"(= (scdr\$n",
" $s)\n",
" (let ((result ((cdr s))))\n",
" (set-cdr! s $) result))\n",
" result)))\n",
"\n",
"(= (first\$n",
" $n s)\n",
" (if (= $n 0)\n",
" ()\n",
" (cons (scar s) (first (- $n 1) (scdr s))))))\n",
"\n",
"(= (nth\$n",
" $n s)\n",
" (if (= $n 0)\n",
" (scar s)\n",
" (nth (- $n 1) (scdr s)))))\n",
"\n",
"(= (smap\$n",
" $f s)\n",
" (scons (f (scar s)) (smap f (scdr s)))))\n",
"\n",
"(= (ones (scons 1 ones))\n",
"\n",
"(= (nats (scons 0 (combine nats + ones)))\n",
"\n",
"(= (combine\$n",
" $s1 op s2)\n",
" (scons (op (scar s1) (scar s2)) (combine (scdr s1) op (scdr s2)))))\n",
"\n",
"(= (fibs (scons 1 (scons 1 (combine fibs + (scdr fibs)))))\n",
"\n",
"(= (facts (scons 1 (combine facts * (scdr nats))))\n",
"\n",
"(= (! $n) (nth $n facts)))"
]
},
{
"cell_type": "code",
"execution_count": 123,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"120"
]
},
"execution_count": 123,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(! 5)"
]
},
{
"cell_type": "code",
"execution_count": 124,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"3628800"
]
},
"execution_count": 124,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(nth 10 facts)"
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"10946"
]
},
"execution_count": 125,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(nth 20 fibs)"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(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)"
]
},
"execution_count": 126,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(first 30 fibs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## dict\$n",
"(dict ((KEY VALUE)...)): creates a Python dictionary\$n",
"(dict ((KEY : VALUE)...)): optional syntax"
]
},
{
"cell_type": "code",
"execution_count": 128,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"{'a': 1, 'b': 2}"
]
},
"execution_count": 128,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(dict ((a : 1)(b : 2)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(dict ((a : 1)(b : 2)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## dir\$n",
"(dir [ITEM]): return items in environment, or, if ITEM is given, the items in module"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(dir)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(dir complex)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## display\$n",
"(display ITEM): display the ITEM as output"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(display \"hello\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## div\$n",
"(div arg0 arg1): quotient procedure for rationals/ints; divides arg0 by arg1 (aliases // and quotient)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(div 5 3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## eq?\n",
"(eq? ITEM1 ITEM2): return #t if ITEM1 is eq to ITEM2, #f otherwise"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(eq? (1 2) (1 2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## equal?\n",
"(equal? ITEM1 ITEM2): return #t if ITEM1 is equal to ITEM2, #f otherwise"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(equal? (1 2) (1 2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## eqv?\n",
"(eqv? ITEM1 ITEM2): return #t if ITEM1 and ITEM2 have the same value"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(eqv? (1 2) (1 2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## error\$n",
"(error NAME FORMATTED-MESSAGE ARGS...): create an exception in NAME with MESSAGE"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(error \"procedure-name\" \"What is ~a\" 'huh?)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## eval\$n",
"(eval LIST): evaluates the LIST as a MeTTa expression"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(eval (+ 1 1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## eval-ast\$n",
"(eval-ast AST): evaluates the Abstract Syntax Tree as a MeTTa expression (see parse and parse-string)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(eval-ast (parse-string \"(+ 1 2)\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## even?\n",
"(even? NUMBER): returns #t if NUMBER is odd, #f otherwise"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(even? 12121)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## exit\$n",
"(exit): "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(exit)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## float\$n",
"(float NUMBER): return NUMBER as a floating point value"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(float 34)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## for-each\$n",
"(for-each PROCEDURE LIST): apply PROCEDURE to each item in LIST; like `map` but don't return results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(for-each $n) (print $n)) (3 4 5))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## format\$n",
"(format STRING ITEM ...): format the string with ITEMS as arguments"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(format \"This uses formatting ~a ~s ~%\" 'apple 'apple)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## func\$n",
"\n",
"Turns a lambda into a Python function.\n",
"\n",
"(func (lambda ...))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(func $n) $n))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(load-as \"test_all.ss\" 'test)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"test"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(get-item test 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"((get-attr 'test 'verify) \"test-name\" 1 = 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"((get 'test '!) 5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## get-stack-trace\$n",
"(get-stack-trace): return the current stack trace"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(get-stack-trace)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## get-item\$n",
"(get-item DICTIONARY ITEM): returns the VALUE of DICTIONARY[ITEM] \n",
"(get-item vector ITEM): returns the VALUE of VECTOR[ITEM] \n",
"(get-attr OBJECT \"ATTRIBUTE\"): return the Python ATTRIBUTE of an OBJECT"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(get-item #(1 2 3) 0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(dict ((a : 100)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(= (a (python-eval \"{'a' : 1}\"))\n",
"(= (b (dict (b : 2)))\n",
"(get-item a 'a)\n",
"(get-item b 'b)\n",
"(get-item a \"a\")\n",
"(get-item b \"b\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(get-item (dict (a : 100) (b : 50)) \"b\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(get-attr (globals) \"keys\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## globals\$n",
"(globals): get global Python environment"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(define-syntax collect\$n",
" [(collect ?exp for ?var in ?list)\n",
" (filter-map $?var) ?exp) $?var) #t) ?list)]\n",
" [(collect ?exp for ?var in ?list if ?condition)\n",
" (filter-map $?var) ?exp) $?var) ?condition) ?list)])\n",
"\n",
"(= (filter-map\$n",
" $f pred? values)\n",
" (if (null? values)\n",
" ()\n",
" (if (pred? (car values))\n",
" (cons (f (car values)) (filter-map f pred? (cdr values)))\n",
" (filter-map f pred? (cdr values))))))\n",
"\n",
"(collect x for x in (map $n) $n) ((get-attr (globals) \"keys\"))) if (string x \"b\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## import\$n",
"\n",
"Use import, import-from, and import-as to load Python libraries. Use load and load-as to load MeTTa files.\n",
"\n",
"(import MODULE...): import host-system modules; MODULEs are strings"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(try \n",
" (import \"math\")\n",
"(catch e \n",
" (import \"Graphics\")))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(math.cos 0.6)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## import-as\$n",
"\n",
"Use import, import-from, and import-as to load Python libraries. Use load and load-as to load MeTTa files.\n",
"\n",
"(import-as MODULE NAME): import a host-system module; MODULE is a string, and NAME is a symbol or string. Use * for NAME to import into toplevel environment"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(import-as \"math\" 'm)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(m.sin 0.6)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## import-from\$n",
"\n",
"Use import, import-from, and import-as to load Python libraries. Use load and load-as to load MeTTa files.\n",
"\n",
"(import-from MODULE NAME...): import from host-system module; MODULE is a string, and NAME is a symbol or string"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(import-from \"math\" \"sin\" \"cos\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(sin .5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## int\$n",
"(int NUMBER): return NUMBER as an integer"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(int 4.5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## integer->char\$n",
"(integer->char INTEGER): return the assocated character of INTEGER"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(integer->char 78)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## iter?\n",
"(iter? ITEM): return #t if ITEM is a iterator, #f otherwise"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(iter? 3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(= (g (globals))\n",
"(iter? (g.keys))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## lambda\$n",
"\n",
"$VARIABLE...) BODY) \n",
"(lambda VARIABLE BODY) - called mu-lambda"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"$n) $n) ;; identity function"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"($i) i) 89)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### mu-lambda"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"((lambda x x) 1 2 3 4 5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"($x . y) (list x y)) 1 2 3 4 5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"($a b . z) (list a b z)) 1 2 3 4 5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"($a b . z) (list a b z)) 1 2 3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"($a b . z) (list a b z)) 1 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(try ($a b . z) (list a b z)) 1)\n",
" (catch e \"not enough arguments given\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## length\$n",
"(length LIST): returns the number of elements in top level of LIST"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(length (1 2 3 4))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## let\$n",
"(let ((VARIABLE VALUE)...) BODY): define variables with a scope in BODY"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(let ((v (vector 1 2 3))) \n",
" (vector-set! v 2 'a) \n",
" v)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(try (let loop (($n 5))\n",
" $n\$n",
" (if (= $n 0)\n",
" (raise \"blastoff!\"))\n",
" (loop (- $n 1)))\n",
"(catch e (get-exception-message e)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## let*\n",
"(let* ...)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(let* ((a 1) (b a))\n",
" b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## letrec\$n",
"(letrec ...)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(letrec \n",
" ((fact $n)\n",
" (if (= $n 1)\n",
" 1\$n",
" (* (fact (- $n 1)) $n)))))\n",
" (fact 5))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## list\$n",
"(list ITEM ...): returns a list composed of all of the items"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(list 1 2 3 4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## list->string\$n",
"(list->string LIST): returns the LIST as a string"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(list->string (#\\1 #\\2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## list->vector\$n",
"(list->vector LIST): returns the LIST as a vector"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(list->vector (1 2 3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## list-ref\$n",
"(list-ref LIST INDEX): returns the item in LIST at INDEX (zero-based)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(list-ref (1 2 3) 0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## list?\n",
"(list? ITEM): return #t if ITEM is a list, #f otherwise"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(list? 123)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## load\$n",
"(load FILENAME...): loads the given FILENAMEs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(load \"sllgen.ss\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## load-as\$n",
"(load-as FILENAME MODULE-NAME): load the filename, putting items in MODULE-NAME namespace"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(load-as \"sllgen.ss\" 'sll)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sll"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## make-set\$n",
"(make-set LIST): returns a list of unique items from LIST"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(make-set (1 2 3 1 2 3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## make-vector\$n",
"(make-vector LENGTH): returns a vector of length LENGTH"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(make-vector 8)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## map\$n",
"(map PROCEDURE LIST...): apply PROCEDURE to each element of LIST; like for-each but returns results.\n",
"\n",
"`map` and `for-each` can iterate over lists and Python iterators."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(map $n) (+ $n 2)) (3 4 5))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(map $n) (print $n)) (3 4 5))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## max\$n",
"(max ...): returns the maximum value from the list of values"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(max (range 10))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## member\$n",
"(member ITEM LIST): return LIST if ITEM in top level of LIST"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(member 10 (range 10))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(member 0 (range 10))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(member \"b\" (\"a\" \"b\" \"c\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## memq\$n",
"(memq ITEM LIST): check for member using eq? in LIST. Returns entire list if found."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(memq 'b (a b c))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(memq 2 (1.0 2.0 3.0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## memv\$n",
"(memv ITEM LIST): check for member using eqv? in LIST. Returns entire list if found."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(memv 2 (1.0 2.0 3.0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## min\$n",
"(min ...): returns the minimum value from the list of values"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(min (range 10))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## mod\$n",
"(mod arg0 arg1): modulo procedure for two arguments (aliases % and modulo)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(mod 7 3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## modulo\$n",
"(modulo arg0 arg1): modulo procedure for two arguments (aliases mod and %)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(modulo 7 3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## newline\$n",
"(newline): displays a new line in output"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(newline)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## not\$n",
"(not ITEM): returns the boolean not of ITEM; ITEM is only #t when #t, otherwise #f"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(not #f)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(not #t)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## null?\n",
"(null? ITEM): return #t if ITEM is empty list, #f otherwise"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(null? ())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(null? (1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## number->string\$n",
"(number->string NUMBER): return NUMBER as a string"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(number->string 76.23)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## number?\n",
"(number? ITEM): return #t if ITEM is a number, #f otherwise"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(number? 7623.3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## odd?\n",
"(odd? NUMBER): returns #t if NUMBER is even, #f otherwise"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(odd? 65)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## or\$n",
"(or ITEM...)\n",
"\n",
"`or` is defined via a macro (define-syntax)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(or #t (/ 8 0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## pair?\n",
"(pair? ITEM): returns #t if ITEM is a cons cell (a proper or improper list). Note that the empty list is a symbol, but not an actual list."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(pair? ())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(pair? (cons 1 2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## parse\$n",
"(parse LIST): parse a list; returns Abstract Syntax Tree (AST)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(parse (+ 2 3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## parse-string\$n",
"(parse-string STRING): parse a string; returns Abstract Syntax Tree (AST)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(parse-string \"(+ 1 2)\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## print\$n",
"(print ITEM): output the ITEM as if by write (alias for write)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(print \"this string\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## printf\$n",
"(printf FORMAT ARGS...): formatted print. Uses standard scheme formatting symbols:\n",
"\n",
"* ~a - shown as if by display\$n",
"* ~s - shown as if by write\$n",
"* ~% - newline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(printf \"many possible ~a ~s\" 'things \"and etc\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## procedure?\n",
"(procedure? ITEM): return #t if ITEM is a procedure, #f otherwise"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(procedure? procedure?)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## property\$n",
"\n",
"Create a Python property. Under development for creating Python classes from MeTTa.\n",
"\n",
"(property NAME VALUE): "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## python-eval\$n",
"(python-eval PYTHON-EXPRESSION [globals [locals]]): return the result of evaluating PYTHON-EXPRESSION string"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(python-eval \"1 + 4\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## python-exec\$n",
"(python-exec PYTHON-STATEMENTS [globals [locals]]): return the result of evaluating PYTHON-STATEMENTS string"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(python-exec \n",
"\"\n",
"x = 1\$n",
"print(x)\n",
"\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## quasiquote\$n",
"(quasiquote LIST): quasiquote allows commas in front of expressions in a list which will be evaluated"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"`(list ,(+ 1 2) 4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## quote\$n",
"\n",
"(quote ITEM): treat ITEM as a literal (data). Can also be written as the single-quote mark"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(quote (1 2 3))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(1 2 3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"'#(1 2 3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"`#(1 ,(+ 2 4))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## quotient\$n",
"(quotient arg0 arg1): quotient procedure for rationals/ints; divides arg0 by arg1 (aliases // and div)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(quotient 7 3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## rac\$n",
"(rac LIST): return the last item of LIST"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(rac (1 2 3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## range\$n",
"(range END), (range START END), or (RANGE START END STEP): (all integers)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(range 10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## rational\$n",
"(rational NUMERATOR DENOMINTAOR): return a rational number"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(rational 3 4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## rdc\$n",
"(rdc LIST): return everything but last item in LIST"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(rdc (1 2 3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## raise\$n",
"\n",
"See try/catch/finally for examples."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## read-string\$n",
"(read-string LIST): returns low-level format used in read. Not generally useful."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(read-string (1 2 3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## record-case\$n",
"(record-case RECORD CASES): match a record to a sequence of CASES"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(record-case (cons 'banana (cons 'orange (cons (* 2 3) ())))\n",
" (apple (a b c) (list c b a r))\n",
" ((cherry banana) (a . b) (list b a 5))\n",
" ((orange) () 'no)\n",
" (%void% 2 3 4))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## remainder\$n",
"(remainder NUMBER1 NUMBER2): returns the remainder after dividing NUMBER1 by NUMBER2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(remainder 6 3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## require\$n",
"(require EXPR): require something to be true. Used with (choose)\n",
"\n",
"See choose for example."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## reset-toplevel-env\$n",
"(reset-toplevel-env): reset the toplevel environment"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(reset-toplevel-env)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## reverse\$n",
"(reverse LIST): return the top-level items in a list in reverse order."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(reverse (1 2 3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## round\$n",
"(round NUMBER): round NUMBER to the nearest integer (may return float)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(round 34.7)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## set!\n",
"(set! VARIABLE VALUE): set a variable to a value\$n",
"\n",
"Note that for objects, you currently need to:\n",
"\n",
"```scheme\$n",
"(set-item! OBJ \"PROPERTY\" VALUE)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(= (x 12)\n",
"(set! x 13)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## set-car!\n",
"(set-car! LIST ITEM): set the car of LIST to be ITEM"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(= (a (1 2 3))\n",
"(set-car! a 7)\n",
"a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## set-cdr!\n",
"(set-cdr! LIST ITEM): set the car of LIST to be ITEM (which is typically a list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(= (a (1 2 3))\n",
"(set-cdr! a (4 5))\n",
"a"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## set-item!\n",
"(set-item! DICTIONARY ITEM VALUE): sets DICTIONARY[ITEM] with VALUE\$n",
"(set-item! VECTOR POSITION VALUE): sets VECTOR[POSITION] with VALUE\$n",
"(set-attr! OBJ \"PROPERTY\" VALUE): sets OBJ.PROPERY with VALUE"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(import \"conx\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(set-attr! conx \"x\" 42)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(get-attr conx \"x\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"conx.x"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(= (d (dict ((a 1)(b 2))))\n",
"(set-item! d 'b 6)\n",
"d"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## snoc\$n",
"(snoc ITEM LIST): cons the ITEM onto the end of LIST"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(snoc '0 (1 2 3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## sort\$n",
"(sort PROCEDURE LIST): sort the list using PROCEDURE to compare items"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(sort $a b) (< a b)) (9 6 2 3 1 2 0))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(sort < (9 6 2 3 1 2 0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## sqrt\$n",
"(sqrt NUMBER): return the square root of NUMBER"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(sqrt 9)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## string\$n",
"(string ITEM): returns ITEM as a string"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(string #\\tab)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## string->list\$n",
"(string->list STRING): string STRING as a list of characters"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(string->list \"hello world!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## string->number\$n",
"(string->number STRING): return STRING as a number"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(string->number \"23.3\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## string->symbol\$n",
"(string->symbol STRING): return STRING as a symbol"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(string->symbol \"apple\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## string-append\$n",
"(string-append STRING1 STRING2): append two strings together"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(string-append \"abc\" \"123\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## string-length\$n",
"(string-length STRING): returns the length of a string"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(string-length \"how long?\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## string-ref\$n",
"(string-ref STRING INDEX): return the character of STRING at position INDEX"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(string-ref \"0123\" 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## string-split\$n",
"(string-split STRING CHAR): return a list with substrings of STRING where split by CHAR"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(string-split \"1234\" #\\2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## string\n",
"(string STRING1 STRING2): compare two strings to see if STRING1 is less than STRING2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(string \"apple\" \"zebra\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## string=?\n",
"(string=? STRING1 STRING2): return #t if STRING1 is the same as STRING2, #f otherwise"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(string=? \"apple\" \"apple\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## string?\n",
"(string? ITEM): return #t if ITEM is a string, #f otherwise"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(string? 'apple)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## substring\$n",
"(substring STRING START [END]): return the substring of STRING starting with position START and ending before END. If END is not provided, it defaults to the length of the STRING"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(substring \"01234\" 1 3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## symbol\$n",
"(symbol STRING): turn STRING into a symbol"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(symbol \"apple\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## symbol->string\$n",
"(symbol->string SYMBOL): return SYMBOL as a string"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(symbol->string 'apple)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## symbol?\n",
"(symbol? ITEM): return #t if ITEM is a symbol, #f otherwise"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(symbol? \"apple\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## try/catch/finally\$n",
"(try ... catch ...): attempt to execute an expression, with a method of catching errors, and running code at end regardless of outcome. Note that the finally clause runs, but the return value comes from try or catch. See below for examples."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(try \n",
" (/ 1 0)\n",
"(catch e \n",
" (printf \"Can't do that!\")))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(try 3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(try 3 \n",
"(finally (= (did-it #t)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"did-it"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(try (/ 3 0)\n",
" (catch e (get-exception-message e))\n",
" (finally (print 'yes 4)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(try (raise \"yes\") \n",
"(catch e (get-exception-message e)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(try (try (raise \"yes\")) \n",
"(catch e (get-exception-message e)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(try (try (begin 'one (raise \"oops\") 'two)) \n",
"(catch e (get-exception-message e)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(* 10 (try (begin 'one (raise \"oops\") 'two)\n",
" (catch ex 3 4)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(* 10 (try (begin 'one 'two 5)\n",
" (catch ex 3 4)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(* 10 (try (begin 'one (raise \"oops\") 5)\n",
" (catch ex (list 'ex: ex) 4)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(try (* 10 (try (begin 'one (raise \"oops\") 5)\n",
" (catch ex (list 'ex: ex) (raise ex) 4))) \n",
"(catch e (get-exception-message e)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(try (* 10 (try (begin 'one (raise \"oops\") 5)\n",
" (catch ex (list 'ex: ex) (raise ex) 4)\n",
" (finally (print 'two 7))))\n",
" (catch e (get-exception-message e)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(try (* 10 (try (begin 'one (raise \"oops\") 5)\n",
" (catch ex (list 'ex: ex) (raise \"bar\") 4)))\n",
" (catch x 'hello 77))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(try 3 \n",
" (finally (print 'hi 4)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(try (div 10 0) \n",
" (catch e (get-exception-message e)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(try (let ((x \n",
" (try (div 10 0)))) x) \n",
" (catch e (get-exception-message e)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(let ((x (try (div 10 2) \n",
" (catch e -1)))) x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(let ((x \n",
" (try (div 10 0) \n",
" (catch e -1)))) x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(let ((x (try (div 10 2) \n",
" (catch e -1) \n",
" (finally (print 'closing-files 42))))) x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(let ((x (try (div 10 0) \n",
" (catch e -1) \n",
" (finally (print 'closing-files 42))))) x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(let ((x (try (div 10 2) \n",
" (finally (print 'closing-files 42))))) x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(try (let ((x (try (div 10 0) \n",
" (catch e -1 (raise \"foo\")) \n",
" (finally (print 'closing-files 42))))) x) (catch e (get-exception-message e)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(try (let ((x (try (div 10 0)\n",
" (catch e -1 (raise \"foo\"))\n",
" (finally (print 'closing-files (raise \"ack\") 42)))))\n",
"x) (catch e (get-exception-message e)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(try (let ((x (try (div 10 0)\n",
" (catch e -1 (raise \"foo\"))\n",
" (finally (print 'closing-files (raise \"ack\") 42)))))\n",
" x)\n",
"(catch e (if (equal? (get-exception-message e) \"ack\") 99 (raise \"doug\")))\n",
"(finally (print 'closing-outer-files)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(try (try (let ((x (try (div 10 0)\n",
" (catch e -1 (raise \"foo\"))\n",
" (finally 'closing-files (raise \"ack\") 42))))\n",
" x)\n",
"(catch e (if (equal? (get-exception-message e) \"foo\") 99 (raise \"doug\")))\n",
"(finally (print 'closing-outer-files))) (catch e (get-exception-message e)))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## typeof\$n",
"(typeof ITEM): returns type of ITEM"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(typeof 23.4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## unparse\$n",
"(unparse AST): given an AST, turn it back into MeTTa code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(unparse (parse (+ 1 2)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## unparse-procedure\$n",
"(unparse-procedure PROCEDURE): given a procedure, show the body as MeTTa code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(unparse-procedure $x) (+ x 1)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## use-lexical-address\$n",
"(use-lexical-address [BOOLEAN]): get lexical-address setting, or set it on/off if BOOLEAN is given"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(use-lexical-address #t)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## use-stack-trace\$n",
"(use-stack-trace BOOLEAN): set stack-trace usage on/off"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(use-stack-trace #t)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## use-tracing\$n",
"(use-tracing [BOOLEAN]): get tracing setting, or set it on/off if BOOLEAN is given"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(use-tracing)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(use-tracing #t)\n",
"(= (fact\$n",
" $n)\n",
" (if (= $n 1)\n",
" 1\$n",
" (* $n (fact (- $n 1))))))\n",
"(fact 1)\n",
"(use-tracing #f)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## vector\$n",
"(vector [ITEMS]...): return ITEMs as a vector"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(vector 1 2 (+ 3 3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## vector->list\$n",
"(vector->list VECTOR): return VECTOR as a list"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(vector->list #(1 2 3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## vector-length\$n",
"(vector-length VECTOR): returns length of VECTOR"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(vector-length #(1 2 3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## vector-ref\$n",
"(vector-ref VECTOR INDEX): "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(vector-ref #(1 2 3) 2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## vector-set!\n",
"(vector-set! VECTOR INDEX VALUE): sets the item at INDEX of VECTOR"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(let ((v #(1 2 3)))\n",
" (vector-set! v 1 4)\n",
" v)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## vector?\n",
"(vector? ITEM): return #t if ITEM is a vector, #f otherwise"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(vector? (1 2 3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## void\$n",
"(void): The null value symbol"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(void)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## zero?\n",
"(zero? NUMBER): return #t if NUMBER is equal to zero, #f otherwise"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(zero? 0.0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Work in progress"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Named parameters and defaults"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"($($n : 1)) $n))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"($($n : 2)) $n))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"($($n : 1)) $n) 3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"($a b c) (list a b c)) 1 2 3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"($a b c) (list a b c)) 1 2 (c : 3))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"($a b c) (list a b c)) 1 (b : 2) (c : 3))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"($a b c) (list a b c)) (a : 1) (b : 2) (c : 3))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"($a b c) (list a b c)) 1 (c : 3) (b : 2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"($($n : 1)) $n) ($n : 3))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Missing functions common in other MeTTa implementations\$n",
"\n",
"The following items are currently missing from Vspace MeTTa. 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 MeTTa `binary` predicate has not been defined."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Exponents\$n",
"* angle\$n",
"* binary\$n",
"* bytevector?\n",
"* ceiling\$n",
"* char-ci<=?\n",
"* char-ci\n",
"* char-ci=?\n",
"* char-ci>=?\n",
"* char-ci>?\n",
"* char-downcase\$n",
"* char-titlecase\$n",
"* char-upcase\$n",
"* char<=?\n",
"* char\n",
"* char>=?\n",
"* char>?\n",
"* complex?\n",
"* cons*\n",
"* cos\$n",
"* denominator\$n",
"* div-and-mod\$n",
"* div0\$n",
"* div0-and-mod0\$n",
"* exact\$n",
"* exact->inexact\$n",
"* exact-integer-sqrt\$n",
"* exact?\n",
"* exp\$n",
"* file-exists\$n",
"* filter\$n",
"* find\$n",
"* finite?\n",
"* floor\$n",
"* gcd\$n",
"* hashtable?\n",
"* hexadecimal\$n",
"* imag-part\$n",
"* inexact\$n",
"* inexact->exact\$n",
"* inexact?\n",
"* inifinite?\n",
"* integer->char\$n",
"* integer-valued?\n",
"* integer?\n",
"* lcm\$n",
"* list-sort\$n",
"* list-tail\$n",
"* log\$n",
"* magnitude\$n",
"* make-polar\$n",
"* make-rectangular\$n",
"* make-string\$n",
"* mod0\$n",
"* nan?\n",
"* negative?\n",
"* numbers\$n",
"* numerator\$n",
"* octal\$n",
"* partition\$n",
"* positive?\n",
"* rational-valued?\n",
"* rational?\n",
"* rationalize\$n",
"* real-part\$n",
"* real-valued?\n",
"* real?\n",
"* remove\$n",
"* remp\$n",
"* remq\$n",
"* remv\$n",
"* sin\$n",
"* string-set!\n",
"* tan\$n",
"* truncate"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(= (tolerance 0.00001)\n",
"\n",
"(= ((fixed-point f first-guess)\n",
" (= ((close-enough? v1 v2)\n",
" (< (abs (- v1 v2)) tolerance))\n",
" (= ((try_ guess)\n",
" (let ((next (f guess)))\n",
" (if (close-enough? guess next)\n",
" next\$n",
" (try_ next))))\n",
" (try_ first-guess))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(fixed-point $guess) (/ (+ guess (/ 2 guess)) 2)) 1.0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(try ($a b . z) (list a b z)) 1)\n",
" (catch e \"not enough arguments given\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(= (try 42)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(+ try 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(+ try (try (/ 3 0) (catch e 1)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(= (if 13)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(+ if 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(try 42)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(case 'begin\$n",
" ((begin) 'foo-begin)\n",
" ((define) 'foo-define)\n",
" (%void% 'foo-else))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(parse \"(case 'begin\$n",
" ((begin) 'foo-begin)\n",
" (%void% 'foo-else))\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(= (a (python-eval \"{'a' : 1}\"))\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(= (b (dict (b : 2)))\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"(print (get-item a 'a))\n",
"(print (get-item b 'b))\n",
"(print (get-item a \"a\"))\n",
"(print (get-item b \"b\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "MeTTa",
"language": "metta",
"name": "metta_kernel"
},
"language_info": {
"codemirror_mode": {
"name": "scheme"
},
"mimetype": "text/x-scheme",
"name": "scheme",
"pygments_lexer": "scheme"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}