government

commit 68160212362299a3894c7ae88d7b2c018f5cc3aa

tree

parent:
fa08368c0d3c24c336bb3fd79706c46fc8e2fd77

nmyk <nick@nmyk.io>

2026-11-14T12:07:34-05:00

add comparisons, lists, strings

rename strappend to strcat

diff --git a/DESIGN.md b/DESIGN.md
index c872fc826cb25b2e2692ea99e2c8c93bc075d2e4..e4cf905845eed00c38aea57d18908d2824464380 100644
--- a/DESIGN.md
+++ b/DESIGN.md
@@ -161,7 +161,7 @@ | `cons` | function | `GOV.0.<id>.99.111.110.115` | 2 args |
 | `car` | function | `GOV.0.<id>.99.97.114` | 1 arg |
 | `cdr` | function | `GOV.0.<id>.99.100.114` | 1 arg |
 | `strlen` | function | `GOV.0.<id>.115.116.114.108.101.110` | 1 arg |
-| `strappend` | function | `GOV.0.<id>.115.116.114.97.112.112.101.110.100` | 0+ args |
+| `strcat` | function | `GOV.0.<id>.115.116.114.99.97.116` | 0+ args |
 | `str?` | function | `GOV.0.<id>.115.116.114.63` | 1 arg |
 
 A ns-0 OID whose name is not in this table is a decode error. Adding a builtin later is backward compatible: it is a new name in the same namespace.
@@ -262,7 +262,7 @@ - **Arithmetic.** `+` and `*` take 0+ args (`(+)` is 0, `(*)` is 1). `-` and `/` take 1+ (`(- x)` negates; `(/ x)` is `(/ 1 x)`). Int with int stays int; any float makes the result float. Integer `/` truncates toward zero. Division by zero is an error.
 - **Ordering.** `<` `>` `<=` `>=` take exactly two numbers (ints and floats mix).
 - **Equality.** `==` `!=` take any two values. Numbers compare numerically (`(== 1 1.0)` is `T`). Strings, symbols and booleans compare by value; pairs compare structurally. Comparing functions is an error.
 - **Boolean.** `NOT` takes one value of any type and returns T only for F.
-- **Strings.** `strlen` returns the number of code points as an integer. `strappend` takes 0+ strings and returns their concatenation (`(strappend)` is `""`). `str?` takes any value and returns `T` if it is a string, else `F`. Non-string arguments to `strlen` or `strappend` are errors.
+- **Strings.** `strlen` returns the number of code points as an integer. `strcat` takes 0+ strings and returns their concatenation (`(strcat)` is `""`). `str?` takes any value and returns `T` if it is a string, else `F`. Non-string arguments to `strlen` or `strcat` are errors.
 - **Lists.** `cons` takes any two values. `car` and `cdr` require a pair; `(car '())` is an error.
 
 ### Program output and errors
@@ -358,7 +358,7 @@ (NOT 0)               ;=> F
 
 ; testdata/strings.lisp
 (strlen "héllo")        ;=> 5
-(strappend "gov" "ern" "ment")   ;=> "government"
+(strcat "gov" "ern" "ment")   ;=> "government"
 (str? "")             ;=> T
 (str? 'foo)           ;=> F
 ```
@@ -438,7 +438,7 @@ | Who is trusted? | The file's own anchor (`-anchor` optional) | Source format |
 | Should programs expire? | Only when built with `-expires` | Civic features |
 | Cert order in the file? | Anchor first, top-down | Source format |
 | OID root? | UUID arc under `2.25` | OID encoding |
-| v1 extras? | No variadic lambdas; `if` without else returns `()`; add `strlen`, `strappend`, `str?` | Semantics, builtin table |
+| v1 extras? | No variadic lambdas; `if` without else returns `()`; add `strlen`, `strcat`, `str?` | Semantics, builtin table |
 | Which X.509 extras? | Effective dates, repeal by CRL, notes, authorship | Civic features |
 
 ## Sources
diff --git a/PLAN.md b/PLAN.md
index 78c6e2c759a033cbd92f3d9213052d67af8cc082..106beba979e8a64e5de679976705f37d12fa477c 100644
--- a/PLAN.md
+++ b/PLAN.md
@@ -67,7 +67,7 @@     - Build: `+ - * /` as builtin function values; int/float promotion; int64 overflow detection; truncating integer division; divide-by-zero and NaN/Inf errors.
     - Learn: numeric towers; checked arithmetic (detecting overflow without wider integers); first-class builtins.
     - Done when: `(+)` is `0`, `(- 5)` is `-5`, `(/ 7 2)` is `3`, `(/ 7.0 2)` is `3.5`, and `(* 9223372036854775807 2)` errors.
 8. **Comparisons, lists and strings** (`eval`)
-    - Build: `< > <= >= == !=`, `NOT`, `cons car cdr`, `strlen strappend str?`.
+    - Build: `< > <= >= == !=`, `NOT`, `cons car cdr`, `strlen strcat str?`.
     - Learn: structural equality on pairs; why comparing functions is refused; strings as code points, not bytes.
     - Done when: `(== '(1 2) '(1 2))` is `T`, `(== 1 1.0)` is `T`, `(strlen "héllo")` is `5`.
 9. **Lambda and application** (`eval`)
diff --git a/eval/arith_test.go b/eval/arith_test.go
index c92dce64c1f0cda0e7c1f0e413fe1fba05bb3c3c..af8447b014468cb7af9c07f48083481aa8fbb7f6 100644
--- a/eval/arith_test.go
+++ b/eval/arith_test.go
@@ -1,14 +1,9 @@
 package eval
 
-import (
-	"errors"
-	"testing"
-
-	"government/sexpr"
-)
+import "testing"
 
 func TestArithmetic(t *testing.T) {
-	tests := []struct{ src, want string }{
+	runEvalCases(t, []evalCase{
 		{"(+)", "0"},
 		{"(+ 5)", "5"},
 		{"(+ 1 2)", "3"},
@@ -56,22 +51,11 @@ 		{"(- -9223372036854775807 1)", "-9223372036854775808"},
 		{"(* -1 9223372036854775807)", "-9223372036854775807"},
 		{"(/ -9223372036854775808 1)", "-9223372036854775808"},
 		{"(* 1.0e308 1.0)", "1.0e+308"},
-	}
-	for _, tt := range tests {
-		t.Run(tt.src, func(t *testing.T) {
-			v, err := evalString(t, tt.src)
-			if err != nil {
-				t.Fatalf("Eval(%s): %v", tt.src, err)
-			}
-			if got := sexpr.Print(v); got != tt.want {
-				t.Errorf("Eval(%s) = %s, want %s", tt.src, got, tt.want)
-			}
-		})
-	}
+	})
 }
 
 func TestArithmeticErrors(t *testing.T) {
-	tests := []struct{ src, code, msg string }{
+	runErrorCases(t, []errorCase{
 		{"(* 9223372036854775807 2)", ErrArith, "integer overflow in *"},
 		{"(+ 9223372036854775807 1)", ErrArith, "integer overflow in +"},
 		{"(- -9223372036854775808 1)", ErrArith, "integer overflow in -"},
@@ -105,17 +89,5 @@ 		// Arguments are evaluated before the call, left to right.
 		{"(+ boom (/ 1 0))", ErrUnbound, "no statute for symbol boom"},
 		{"(+ (/ 1 0) boom)", ErrArith, "division by zero"},
 		{"(+ 1 (if))", ErrSyntax, "if takes 2 or 3 operands, got 0"},
-	}
-	for _, tt := range tests {
-		t.Run(tt.src, func(t *testing.T) {
-			_, err := evalString(t, tt.src)
-			var e *Error
-			if !errors.As(err, &e) {
-				t.Fatalf("Eval(%s) error = %v, want *Error", tt.src, err)
-			}
-			if e.Code != tt.code || e.Msg != tt.msg {
-				t.Errorf("Eval(%s) error = %s: %s, want %s: %s", tt.src, e.Code, e.Msg, tt.code, tt.msg)
-			}
-		})
-	}
+	})
 }
diff --git a/eval/builtin.go b/eval/builtin.go
index b5695b3e6755b4bb9ff3e5ee0f8e20251fd411cb..c25c4574fa2dc86af5414ee587aebc180eb8a477 100644
--- a/eval/builtin.go
+++ b/eval/builtin.go
@@ -25,6 +25,19 @@ 	{name: "+", minArgs: 0, maxArgs: -1, fn: add},
 	{name: "-", minArgs: 1, maxArgs: -1, fn: subtract},
 	{name: "*", minArgs: 0, maxArgs: -1, fn: multiply},
 	{name: "/", minArgs: 1, maxArgs: -1, fn: divide},
+	{name: "<", minArgs: 2, maxArgs: 2, fn: less},
+	{name: ">", minArgs: 2, maxArgs: 2, fn: greater},
+	{name: "<=", minArgs: 2, maxArgs: 2, fn: lessEq},
+	{name: ">=", minArgs: 2, maxArgs: 2, fn: greaterEq},
+	{name: "==", minArgs: 2, maxArgs: 2, fn: equal},
+	{name: "!=", minArgs: 2, maxArgs: 2, fn: notEqual},
+	{name: "NOT", minArgs: 1, maxArgs: 1, fn: not},
+	{name: "cons", minArgs: 2, maxArgs: 2, fn: cons},
+	{name: "car", minArgs: 1, maxArgs: 1, fn: car},
+	{name: "cdr", minArgs: 1, maxArgs: 1, fn: cdr},
+	{name: "strlen", minArgs: 1, maxArgs: 1, fn: strlen},
+	{name: "strcat", minArgs: 0, maxArgs: -1, fn: strcat},
+	{name: "str?", minArgs: 1, maxArgs: 1, fn: isString},
 }
 
 // Builtins returns the top-level environment: every builtin bound to its
diff --git a/eval/compare.go b/eval/compare.go
new file mode 100644
index 0000000000000000000000000000000000000000..baf85c1302057ddf3480fffa5a3441e4256e86bc
--- /dev/null
+++ b/eval/compare.go
@@ -0,0 +1,138 @@
+package eval
+
+import (
+	"math"
+
+	"government/sexpr"
+)
+
+func less(args []sexpr.Value) (sexpr.Value, error) {
+	return order("<", args, func(c int) bool { return c < 0 })
+}
+func greater(args []sexpr.Value) (sexpr.Value, error) {
+	return order(">", args, func(c int) bool { return c > 0 })
+}
+func lessEq(args []sexpr.Value) (sexpr.Value, error) {
+	return order("<=", args, func(c int) bool { return c <= 0 })
+}
+func greaterEq(args []sexpr.Value) (sexpr.Value, error) {
+	return order(">=", args, func(c int) bool { return c >= 0 })
+}
+
+func order(name string, args []sexpr.Value, holds func(cmp int) bool) (sexpr.Value, error) {
+	for i, a := range args {
+		if err := checkNumber(name, i, a); err != nil {
+			return nil, err
+		}
+	}
+	return sexpr.Bool(holds(compareNumbers(args[0], args[1]))), nil
+}
+
+func equal(args []sexpr.Value) (sexpr.Value, error) {
+	eq, err := valuesEqual(args[0], args[1])
+	return sexpr.Bool(eq), err
+}
+
+func notEqual(args []sexpr.Value) (sexpr.Value, error) {
+	eq, err := valuesEqual(args[0], args[1])
+	return sexpr.Bool(!eq), err
+}
+
+func not(args []sexpr.Value) (sexpr.Value, error) {
+	return sexpr.Bool(!isTrue(args[0])), nil
+}
+
+// valuesEqual is structural equality. Numbers compare by numeric value
+// across Int and Float, other atoms by value, pairs element by element.
+// Values of different kinds are unequal, so (== 1 "1") is F.
+//
+// Functions cannot be compared: closure equality would need either identity
+// (two identical lambdas unequal) or deciding whether two programs compute
+// the same thing (impossible), and neither is worth exposing.
+func valuesEqual(a, b sexpr.Value) (bool, error) {
+	for {
+		if _, ok := a.(sexpr.Function); ok {
+			return false, errorf(ErrType, "cannot compare functions")
+		}
+		if _, ok := b.(sexpr.Function); ok {
+			return false, errorf(ErrType, "cannot compare functions")
+		}
+		pa, aIsPair := a.(*sexpr.Pair)
+		pb, bIsPair := b.(*sexpr.Pair)
+		if !aIsPair || !bIsPair {
+			return atomsEqual(a, b), nil
+		}
+		// Recurse into the cars, loop along the cdrs, so long lists use
+		// constant stack.
+		eq, err := valuesEqual(pa.Car, pb.Car)
+		if err != nil || !eq {
+			return false, err
+		}
+		a, b = pa.Cdr, pb.Cdr
+	}
+}
+
+func atomsEqual(a, b sexpr.Value) bool {
+	if isNumber(a) && isNumber(b) {
+		return compareNumbers(a, b) == 0
+	}
+	return a == b
+}
+
+func isNumber(v sexpr.Value) bool {
+	switch v.(type) {
+	case sexpr.Int, sexpr.Float:
+		return true
+	}
+	return false
+}
+
+// compareNumbers returns -1, 0 or 1. It is exact across Int and Float:
+// converting a large Int to float64 can round it (2^53 + 1 becomes 2^53),
+// so mixed comparisons go through compareIntFloat instead.
+func compareNumbers(a, b sexpr.Value) int {
+	switch a := a.(type) {
+	case sexpr.Int:
+		switch b := b.(type) {
+		case sexpr.Int:
+			return cmp(a, b)
+		case sexpr.Float:
+			return compareIntFloat(int64(a), float64(b))
+		}
+	case sexpr.Float:
+		switch b := b.(type) {
+		case sexpr.Int:
+			return -compareIntFloat(int64(b), float64(a))
+		case sexpr.Float:
+			return cmp(a, b)
+		}
+	}
+	panic("eval: compareNumbers on non-numbers")
+}
+
+// compareIntFloat compares exactly by splitting f into its integer part,
+// which is compared as an int64, and its fraction, which breaks ties.
+func compareIntFloat(i int64, f float64) int {
+	const two63 = 9223372036854775808.0 // 2^63, exactly representable
+	switch {
+	case f >= two63:
+		return -1
+	case f < -two63:
+		return 1
+	}
+	whole := math.Trunc(f)
+	if c := cmp(i, int64(whole)); c != 0 {
+		return c
+	}
+	return cmp(0, f-whole)
+}
+
+func cmp[T int64 | sexpr.Int | sexpr.Float | float64](a, b T) int {
+	switch {
+	case a < b:
+		return -1
+	case a > b:
+		return 1
+	}
+	return 0
+}
diff --git a/eval/compare_test.go b/eval/compare_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..38839d833a7645711f2e679ba94d04e5b79518ed
--- /dev/null
+++ b/eval/compare_test.go
@@ -0,0 +1,108 @@
+package eval
+
+import "testing"
+
+func TestComparisons(t *testing.T) {
+	runEvalCases(t, []evalCase{
+		{"(< 1 2)", "T"},
+		{"(< 2 1)", "F"},
+		{"(< 1 1)", "F"},
+		{"(<= 1 1)", "T"},
+		{"(> 2 1)", "T"},
+		{"(>= 1 2)", "F"},
+		{"(>= 2 2)", "T"},
+		{"(< -1.5 -1)", "T"},
+		{"(< 1 1.5)", "T"},
+		{"(> 2.0 1)", "T"},
+		{"(<= 1.0 1)", "T"},
+
+		// Exact across Int and Float, even where float64 rounds.
+		// 9007199254740993 is 2^53 + 1, which as a float64 is 2^53.
+		{"(> 9007199254740993 9007199254740992.0)", "T"},
+		{"(== 9007199254740993 9007199254740992.0)", "F"},
+		{"(== 9007199254740992 9007199254740992.0)", "T"},
+		{"(< 9223372036854775807 9223372036854775808.0)", "T"},
+		{"(> -9223372036854775808 -9223372036854777856.0)", "T"},
+		{"(== -9223372036854775808 -9223372036854775808.0)", "T"},
+		{"(< 1 1.0000000000000002)", "T"},
+		{"(> -1 -1.0000000000000002)", "T"},
+		{"(< -0.5 0)", "T"},
+		{"(> 0.5 0)", "T"},
+
+		{"(== 1 1)", "T"},
+		{"(== 1 2)", "F"},
+		{"(== 1 1.0)", "T"},
+		{"(== 1.0 1)", "T"},
+		{"(!= 1 1.0)", "F"},
+		{`(== "a" "a")`, "T"},
+		{`(== "a" "b")`, "F"},
+		{`(== "" "")`, "T"},
+		{"(== 'a 'a)", "T"},
+		{"(== 'a 'b)", "F"},
+		{"(== T T)", "T"},
+		{"(== T F)", "F"},
+		{"(== () ())", "T"},
+		{"(== '() ())", "T"},
+
+		// Different kinds are never equal.
+		{`(== 1 "1")`, "F"},
+		{"(== 'T T)", "T"},
+		{`(== 'a "a")`, "F"},
+		{"(== F ())", "F"},
+		{"(== 0 F)", "F"},
+		{"(!= 0 ())", "T"},
+
+		// Pairs compare structurally.
+		{"(== '(1 2 3) '(1 2 3))", "T"},
+		{"(== '(1 2 3) '(1 2))", "F"},
+		{"(== '(1 2) '(1 2 3))", "F"},
+		{"(== '(1 (2 3)) '(1 (2 3)))", "T"},
+		{"(== '(1 (2 3)) '(1 (2 4)))", "F"},
+		{"(== '(1 . 2) '(1 . 2))", "T"},
+		{"(== '(1 . 2) '(1 2))", "F"},
+		{"(== '(1 2.0) '(1.0 2))", "T"},
+		{"(== (cons 1 2) '(1 . 2))", "T"},
+		{"(== '(a) 'a)", "F"},
+		{"(!= '(1 2) '(1 2))", "F"},
+
+		// Only F is false.
+		{"(NOT F)", "T"},
+		{"(NOT T)", "F"},
+		{"(NOT 0)", "F"},
+		{"(NOT ())", "F"},
+		{`(NOT "")`, "F"},
+		{"(NOT (NOT F))", "F"},
+		{"(NOT (< 2 1))", "T"},
+	})
+}
+
+func TestComparisonErrors(t *testing.T) {
+	runErrorCases(t, []errorCase{
+		{"(< 1)", ErrArity, "< takes 2 arguments, got 1"},
+		{"(< 1 2 3)", ErrArity, "< takes 2 arguments, got 3"},
+		{"(==)", ErrArity, "== takes 2 arguments, got 0"},
+		{"(NOT)", ErrArity, "NOT takes 1 argument, got 0"},
+		{"(NOT T F)", ErrArity, "NOT takes 1 argument, got 2"},
+		{`(< "a" "b")`, ErrType, `argument 1 of < must be a number, got "a"`},
+		{"(>= 1 'x)", ErrType, "argument 2 of >= must be a number, got x"},
+		{"(< 1 ())", ErrType, "argument 2 of < must be a number, got ()"},
+
+		{"(== + +)", ErrType, "cannot compare functions"},
+		{"(== + 1)", ErrType, "cannot compare functions"},
+		{"(!= 1 car)", ErrType, "cannot compare functions"},
+		{"(== (cons + 1) (cons + 1))", ErrType, "cannot compare functions"},
+	})
+}
+
+func TestEqualityOnLongLists(t *testing.T) {
+	src := "(== '(" + repeat("1 ", 100_000) + ") '(" + repeat("1 ", 100_000) + "))"
+	runEvalCases(t, []evalCase{{src, "T"}})
+}
+
+func repeat(s string, n int) string {
+	b := make([]byte, 0, len(s)*n)
+	for i := 0; i < n; i++ {
+		b = append(b, s...)
+	}
+	return string(b)
+}
diff --git a/eval/eval_test.go b/eval/eval_test.go
index 266903ff30d77f151c42b7a3a35adb975aaaf1d5..3f8e08a458a25cadb77d5fb9e587e1d95b58bef6 100644
--- a/eval/eval_test.go
+++ b/eval/eval_test.go
@@ -29,8 +29,43 @@ 	}
 	return Eval(forms[0], testEnv())
 }
 
+type evalCase struct{ src, want string }
+
+type errorCase struct{ src, code, msg string }
+
+func runEvalCases(t *testing.T, tests []evalCase) {
+	t.Helper()
+	for _, tt := range tests {
+		t.Run(tt.src, func(t *testing.T) {
+			v, err := evalString(t, tt.src)
+			if err != nil {
+				t.Fatalf("Eval(%s): %v", tt.src, err)
+			}
+			if got := sexpr.Print(v); got != tt.want {
+				t.Errorf("Eval(%s) = %s, want %s", tt.src, got, tt.want)
+			}
+		})
+	}
+}
+
+func runErrorCases(t *testing.T, tests []errorCase) {
+	t.Helper()
+	for _, tt := range tests {
+		t.Run(tt.src, func(t *testing.T) {
+			_, err := evalString(t, tt.src)
+			var e *Error
+			if !errors.As(err, &e) {
+				t.Fatalf("Eval(%s) error = %v, want *Error", tt.src, err)
+			}
+			if e.Code != tt.code || e.Msg != tt.msg {
+				t.Errorf("Eval(%s) error = %s: %s, want %s: %s", tt.src, e.Code, e.Msg, tt.code, tt.msg)
+			}
+		})
+	}
+}
+
 func TestEval(t *testing.T) {
-	tests := []struct{ src, want string }{
+	runEvalCases(t, []evalCase{
 		// Atoms evaluate to themselves.
 		{"42", "42"},
 		{"-1.5", "-1.5"},
@@ -73,22 +108,11 @@
 		{"(if (if F T F) 1 2)", "2"},
 		{"(if T (if F 1 2) 3)", "2"},
 		{"(if T 'x)", "x"},
-	}
-	for _, tt := range tests {
-		t.Run(tt.src, func(t *testing.T) {
-			v, err := evalString(t, tt.src)
-			if err != nil {
-				t.Fatalf("Eval(%s): %v", tt.src, err)
-			}
-			if got := sexpr.Print(v); got != tt.want {
-				t.Errorf("Eval(%s) = %s, want %s", tt.src, got, tt.want)
-			}
-		})
-	}
+	})
 }
 
 func TestEvalErrors(t *testing.T) {
-	tests := []struct{ src, code, msg string }{
+	runErrorCases(t, []errorCase{
 		{"boom", ErrUnbound, "no statute for symbol boom"},
 		{"(if boom 1 2)", ErrUnbound, "no statute for symbol boom"},
 		{"(if T boom 2)", ErrUnbound, "no statute for symbol boom"},
@@ -110,17 +134,5 @@ 		{"(x)", ErrNotAFunction, "1 is not a function"},
 		{`("f" 1)`, ErrNotAFunction, `"f" is not a function`},
 		{"(boom 1)", ErrUnbound, "no statute for symbol boom"},
 		{"((if T 1 2) 3)", ErrNotAFunction, "1 is not a function"},
-	}
-	for _, tt := range tests {
-		t.Run(tt.src, func(t *testing.T) {
-			_, err := evalString(t, tt.src)
-			var e *Error
-			if !errors.As(err, &e) {
-				t.Fatalf("Eval(%s) error = %v, want *Error", tt.src, err)
-			}
-			if e.Code != tt.code || e.Msg != tt.msg {
-				t.Errorf("Eval(%s) error = %s: %s, want %s: %s", tt.src, e.Code, e.Msg, tt.code, tt.msg)
-			}
-		})
-	}
+	})
 }
diff --git a/eval/lists.go b/eval/lists.go
new file mode 100644
index 0000000000000000000000000000000000000000..0e79ae57c7bdf9963efecdbd6845013abb645c1d
--- /dev/null
+++ b/eval/lists.go
@@ -0,0 +1,55 @@
+package eval
+
+import (
+	"strings"
+	"unicode/utf8"
+
+	"government/sexpr"
+)
+
+func cons(args []sexpr.Value) (sexpr.Value, error) {
+	return sexpr.Cons(args[0], args[1]), nil
+}
+
+func car(args []sexpr.Value) (sexpr.Value, error) {
+	p, ok := args[0].(*sexpr.Pair)
+	if !ok {
+		return nil, errorf(ErrType, "car needs a pair, got %s", sexpr.Print(args[0]))
+	}
+	return p.Car, nil
+}
+
+func cdr(args []sexpr.Value) (sexpr.Value, error) {
+	p, ok := args[0].(*sexpr.Pair)
+	if !ok {
+		return nil, errorf(ErrType, "cdr needs a pair, got %s", sexpr.Print(args[0]))
+	}
+	return p.Cdr, nil
+}
+
+// strlen counts code points, so (strlen "héllo") is 5 although the UTF-8
+// encoding is 6 bytes.
+func strlen(args []sexpr.Value) (sexpr.Value, error) {
+	s, ok := args[0].(sexpr.String)
+	if !ok {
+		return nil, errorf(ErrType, "argument 1 of strlen must be a string, got %s", sexpr.Print(args[0]))
+	}
+	return sexpr.Int(utf8.RuneCountInString(string(s))), nil
+}
+
+func strcat(args []sexpr.Value) (sexpr.Value, error) {
+	var b strings.Builder
+	for i, a := range args {
+		s, ok := a.(sexpr.String)
+		if !ok {
+			return nil, errorf(ErrType, "argument %d of strcat must be a string, got %s", i+1, sexpr.Print(a))
+		}
+		b.WriteString(string(s))
+	}
+	return sexpr.String(b.String()), nil
+}
+
+func isString(args []sexpr.Value) (sexpr.Value, error) {
+	_, ok := args[0].(sexpr.String)
+	return sexpr.Bool(ok), nil
+}
diff --git a/eval/lists_test.go b/eval/lists_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..23dedbf7f9ab45e321191a14f3f4ff45c068adfd
--- /dev/null
+++ b/eval/lists_test.go
@@ -0,0 +1,75 @@
+package eval
+
+import "testing"
+
+func TestLists(t *testing.T) {
+	runEvalCases(t, []evalCase{
+		{"(cons 1 2)", "(1 . 2)"},
+		{"(cons 1 ())", "(1)"},
+		{"(cons 1 '(2 3))", "(1 2 3)"},
+		{"(cons '(1) '(2))", "((1) 2)"},
+		{"(cons (+ 1 1) (cons 3 ()))", "(2 3)"},
+		{"(cons + ())", "(#<builtin +>)"},
+		{"(car '(1 2 3))", "1"},
+		{"(cdr '(1 2 3))", "(2 3)"},
+		{"(cdr '(1))", "()"},
+		{"(car '((1 2) 3))", "(1 2)"},
+		{"(car (cdr '(1 2 3)))", "2"},
+		{"(car '(1 . 2))", "1"},
+		{"(cdr '(1 . 2))", "2"},
+		{"(car (cons 'a 'b))", "a"},
+		{"(cdr (cons 'a 'b))", "b"},
+
+		// The tripwire used throughout the tests: car of () is an error, so
+		// it only appears in branches that must not run.
+		{"(if T 1 (car '()))", "1"},
+	})
+}
+
+func TestListErrors(t *testing.T) {
+	runErrorCases(t, []errorCase{
+		{"(car '())", ErrType, "car needs a pair, got ()"},
+		{"(cdr '())", ErrType, "cdr needs a pair, got ()"},
+		{"(car 1)", ErrType, "car needs a pair, got 1"},
+		{`(cdr "ab")`, ErrType, `cdr needs a pair, got "ab"`},
+		{"(car (cdr '(1)))", ErrType, "car needs a pair, got ()"},
+		{"(cons 1)", ErrArity, "cons takes 2 arguments, got 1"},
+		{"(cons 1 2 3)", ErrArity, "cons takes 2 arguments, got 3"},
+		{"(car)", ErrArity, "car takes 1 argument, got 0"},
+		{"(cdr '(1) '(2))", ErrArity, "cdr takes 1 argument, got 2"},
+	})
+}
+
+func TestStrings(t *testing.T) {
+	runEvalCases(t, []evalCase{
+		{`(strlen "")`, "0"},
+		{`(strlen "hello")`, "5"},
+		{`(strlen "héllo")`, "5"},
+		{`(strlen "😀")`, "1"},
+		{`(strlen "a\nb")`, "3"},
+		{`(strcat)`, `""`},
+		{`(strcat "gov")`, `"gov"`},
+		{`(strcat "gov" "ern" "ment")`, `"government"`},
+		{`(strcat "" "a" "")`, `"a"`},
+		{`(strcat "é" "😀")`, `"é😀"`},
+		{`(strlen (strcat "ab" "cd"))`, "4"},
+		{`(str? "")`, "T"},
+		{`(str? "x")`, "T"},
+		{`(str? 'x)`, "F"},
+		{`(str? 1)`, "F"},
+		{`(str? ())`, "F"},
+		{`(str? '("x"))`, "F"},
+		{`(str? str?)`, "F"},
+	})
+}
+
+func TestStringErrors(t *testing.T) {
+	runErrorCases(t, []errorCase{
+		{"(strlen 'abc)", ErrType, "argument 1 of strlen must be a string, got abc"},
+		{"(strlen 1)", ErrType, "argument 1 of strlen must be a string, got 1"},
+		{`(strcat "a" 1)`, ErrType, "argument 2 of strcat must be a string, got 1"},
+		{`(strcat "a" "b" '(c))`, ErrType, "argument 3 of strcat must be a string, got (c)"},
+		{"(strlen)", ErrArity, "strlen takes 1 argument, got 0"},
+		{`(str? "a" "b")`, ErrArity, "str? takes 1 argument, got 2"},
+	})
+}