Lists and Objects
Lists and Objects
Lists are central to MemCP's Scheme dialect: they represent ordinary collections, associative objects and executable code. This tutorial explains the programming model; Lists and Associative Lists / Dictionaries provide the generated function reference.
List values
A quoted list is data. Mapping and filtering return new lists; reduction combines them into one value.
'(1 2 3) (cons 0 '(1 2 3)) /* (0 1 2 3) */ (append '(1 2 3) 4 5) /* (1 2 3 4 5) */ (has? '(1 2 3) 2) /* true */ (filter '(1 2 3) (lambda (x) (< x 3))) /* (1 2) */ (map '(1 2 3) (lambda (x) (* x 2))) /* (2 4 6) */ (reduce '(1 2 3) + 0) /* 6 */
Choose a correct neutral value and an associative reducer when work may run in parallel. Order-sensitive string concatenation, floating-point sums and callbacks with I/O require particular care.
Associative lists as objects
An object is a flat list of alternating keys and values. Calling it with a key performs lookup. Updates are functional: set_assoc returns a new value instead of mutating the old list.
(set obj '("a" 1 "b" 2 "c" 3))
(obj "a")
(set obj (set_assoc obj "a" 5))
(filter_assoc obj (lambda (key value) (not (equal? key "c"))))
(map_assoc obj (lambda (key value) (* value 2)))
(reduce_assoc obj (lambda (acc key value) (+ acc value)) 0)
Use has_assoc? when absence differs from a stored nil. Request objects, JSON objects, SQL result rows and sessions may expose a similar callable lookup style, but they are not necessarily represented by the same physical type.
Lists as generated code
Scheme code is list-shaped data. Quoting controls which computation happens while building a program and which is delayed until eval.
(set program '('print "Hello World"))
(eval program)
(set add_expression '('+ 4 5))
(eval add_expression) /* 9 */
Symbols naming delayed procedures remain quoted. Values that must be computed while building the outer expression are embedded without an extra quote. Generated lambdas need a quoted lambda symbol, a parameter list and exactly one body; use begin for several forms.
The optimizer may fuse safe list operations or JIT-compile supported hot procedures, but observable functional semantics remain unchanged. See Introduction to Scheme, JIT Compilation and Parallel Computing.