Scheme is a programming language that is a descendant of Lisp, one of the earliest programming languages. Lisp (which stands for List Processing) was specified in 1958.
Like Python, Scheme is also an imperative language. However, Scheme is based on expressions rather than statements.
For these notebooks, we will use Calysto Scheme.
In [1]:
1
Out[1]:
In [17]:
2
Out[17]:
In [19]:
-3
Out[19]:
In [2]:
3.14
Out[2]:
In [18]:
"apple"
Out[18]:
So far, Scheme looks exactly like Python.
In [14]:
(quote apple)
Out[14]:
In [4]:
'apple
Out[4]:
In [9]:
+
Out[9]:
In [15]:
(+ 1 2)
Out[15]:
In [16]:
(1 + 2)
Calysto error messages appear very similar to Python's error messages and are largely interpreted in the same manner. The best way to read them is to start at the bottom. In the above example, the specific error message is:
RunTimeError: attempt to apply non-procedure '1'
Above the error message is the "traceback" also called the "call stack". This is a representation of the sequence of procedure calls that lead to the error. If the procedure call originated from code from a file, the filename would be listed after the word "File" on each line. If the procedure call originated from a notebook cell, then the word "In" followed by the cell execution number in square brackets.
In [7]:
(quote (1 2 3))
Out[7]:
In [8]:
'(1 2 3)
Out[8]:
In [12]:
'(1 + 1)
Out[12]:
In [22]:
void
Out[22]:
In [23]:
(void)
In [5]:
(= 1 1)
Out[5]:
In [4]:
(= 2 3)
Out[4]:
In [7]:
(= 3.1415 3.1415)
Out[7]:
The = predicate cannot be used on any other type of data:
In [3]:
(= '() '())
In [9]:
(eq? '(1) '(1))
Out[9]:
Unlike Python, the empty list is a unique object:
In [8]:
(eq? '() '())
Out[8]:
In [16]:
(eq? "a" "a")
Out[16]:
The behavior of the eq?
predicate on numbers and strings is dependent on the implementation of Scheme. In fact, in Calysto Scheme, it is dependent on the value of the type.
For example, the number 1 is the same place in memory:
In [18]:
(eq? 1 1)
Out[18]:
However, for other values of numbers, it might not be the same place in memory:
In [1]:
(eq? 57663463467 57663463467)
Out[1]:
Because eq? can give different values for numbers and strings depending on the implementation, it is generally not useful. Rather, you should use the eqv?
predicate. It does what you expect: for non-numbers and non-strings it checks memory, but it gives #t if the two values of strings and numbers are the same.
In [2]:
(eqv? 57663463467 57663463467)
Out[2]:
In [3]:
(eqv? 1.1 1.1)
Out[3]:
In [4]:
(eqv? "a" "a")
Out[4]:
In [25]:
(number? 57663463467)
Out[25]:
In [28]:
(typeof 57663463467)
Out[28]:
In [7]:
(quote "hello")
Out[7]:
In [10]:
(eq? 'a 'a)
Out[10]:
In [2]:
(+ (complex 2 5) 1)
Out[2]:
In [ ]: