Introduction to Scheme
When you run ./memcp
, you will be dropped at a scheme shell like this:
memcp Copyright (C) 2023, 2024 Carl-Philip Hänsch This program comes with ABSOLUTELY NO WARRANTY; This is free software, and you are welcome to redistribute it under certain conditions; loading storage /tmp/x/system/bdf64a22-6315-463c-bbf7-329317ad50ed-id of type 10 loading storage /tmp/x/system/bdf64a22-6315-463c-bbf7-329317ad50ed-username of type 20 loading storage /tmp/x/system/bdf64a22-6315-463c-bbf7-329317ad50ed-password of type 20 Welcome to memcp performing unit tests ... finished unit tests test result: 15/15 all tests succeeded. Initializing SQL frontend MySQL server listening on port 3307 (connect with `mysql -P 3307 -u root -p` using password 'admin') listening on http://localhost:4321 Type (help) to show help >
Scheme is a functional programming language and a subset of LISP. Every expression is either a primitive value or a list.
To understand the semantics of Scheme, take a look at the following Scheme REPL session:
> 12 = 12 > "hi" = "hi" > (+ 1 2) = 3 > (+ 1 2 3) = 6 > (+ 2 (* 2 2)) = 6 > (+ 2 (* 2 4)) = 10 > (concat "Hello" "World") = "HelloWorld"
Syntax
Function Call
A function call has the following format:
(functionname param1 param2 param3 ...)
Function calls start with (
, contain one function and zero or more parameters separated by space .
Some basic functions are + - * / print concat
. For more consult the manual with (help)
.
Literals
The following literals are allowed:
- Number Literals:
1 2 3 56 7.5 4.3e20 -6.123e-3
- String Literals:
"Hello World" "First Line\nSecond Line" "he said: \"what?\" and smiled"
- Function names:
print
- Symbol literals
'print
- List literals
'(1 2 3) '("a" "b" "c")
- Associative Array literals
'("key" "value" "size" 45 "name" "Peter" "sublist" '(1 2 3))
- associative arrays are lists with key value pairs flattened down to a one dimensional list of an even number of items. - Lambdas:
(lambda (param1 param2 ...) body)
Lambda Functions
Scheme allows for creating lambda functions which enclose their own scope:
(define decrease-by-one (lambda (number) (- number 1)))
The function can now be called from scheme:
> (decrease-by-one 5) = 4
Lambda functions can be passed to other functions as values. They enclose your original scope, so you can use variables from the outside inside your function.
Deeper Topics into Scheme
You can deep-dive into the following topcis: