summaryrefslogtreecommitdiffstats
path: root/tests/011
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2021-02-04 01:43:20 -0800
committerKaz Kylheku <kaz@kylheku.com>2021-02-04 01:43:20 -0800
commit8132c60cdb613e27eb2b8e58b8d1ea9ae01a615c (patch)
tree62d32a6245964cb20ed4febfa743afd3862c4b3e /tests/011
parent8bc0ac3a54d248ba2d4a8a045dc2bc0619a60886 (diff)
downloadtxr-8132c60cdb613e27eb2b8e58b8d1ea9ae01a615c.tar.gz
txr-8132c60cdb613e27eb2b8e58b8d1ea9ae01a615c.tar.bz2
txr-8132c60cdb613e27eb2b8e58b8d1ea9ae01a615c.zip
matcher: lambda-match: redoc, bugfix, test-cases
* share/txr/stdlib/match.tl (expand-lambda-match): In a case that takes the maximum number of fixed args and no dotted pattern, in a function that is variadic, we must assert that the rest parameter is nil: there are no additional arguments. In the lambda args, we must generate the colon that separates the optional arguments. * tests/011/patmatch.tl: basic test cases for lambda-match and defun-match. * txr.1: lambda-match and defun-match redocumented, with examples.
Diffstat (limited to 'tests/011')
-rw-r--r--tests/011/patmatch.tl50
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/011/patmatch.tl b/tests/011/patmatch.tl
index 46fc2719..5dd735b9 100644
--- a/tests/011/patmatch.tl
+++ b/tests/011/patmatch.tl
@@ -186,3 +186,53 @@
(test (let ((h #H(() (a 1) (b 2))))
(when-match @[h x @(oddp y)] 'a (list x y)))
(a 1))
+
+(test
+ (let ((f (lambda-match
+ (() (list 0 :args))
+ ((@a) (list 1 :arg a))
+ ((@a @b) (list 2 :args a b))
+ ((@a @b . @c) (list* '> 2 :args a b c)))))
+ (list [f] [f 1] [f 1 2] [f 1 2 3]))
+ ((0 :args) (1 :arg 1) (2 :args 1 2) (> 2 :args 1 2 3)))
+
+(test
+ [(lambda-match
+ ((0 1) :zero-one)
+ ((1 0) :one-zero)
+ ((@x @y) :no-match)) 1 0]
+ :one-zero)
+
+(test
+ [(lambda-match
+ ((0 1) :zero-one)
+ ((1 0) :one-zero)
+ ((@x @y) :no-match)) 1 1]
+ :no-match)
+
+(test
+ [(lambda-match
+ ((0 1) :zero-one)
+ ((1 0) :one-zero)
+ ((@x @y) :no-match)) 1 2 3]
+ :error)
+
+(defun-match fib
+ ((0) 1)
+ ((1) 1)
+ ((@x) (+ (fib (pred x)) (fib (ppred x)))))
+
+(test (fib 0) 1)
+(test (fib 1) 1)
+(test (fib 2) 2)
+(test (fib 3) 3)
+(test (fib 4) 5)
+(test (fib 5) 8)
+
+(defun-match ack
+ ((0 @n) (+ n 1))
+ ((@m 0) (ack (- m 1) 1))
+ ((@m @n) (ack (- m 1) (ack m (- n 1)))))
+
+(test (ack 1 1) 3)
+(test (ack 2 2) 7)