From 76cd2b919aa5ed2c9be56b39b8bc49c4c9bdae99 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Thu, 15 Mar 2018 18:30:47 -0400 Subject: [PATCH] Handle dotted list in length function --- lisp/lang/functions.lisp | 7 ------- lisp/list/append.lisp | 7 +++++++ src/function/builtin/cons/LENGTH.java | 3 +++ test/function/builtin/cons/LENGTHTest.java | 6 ++++++ 4 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 lisp/list/append.lisp diff --git a/lisp/lang/functions.lisp b/lisp/lang/functions.lisp index c684278..985b765 100644 --- a/lisp/lang/functions.lisp +++ b/lisp/lang/functions.lisp @@ -37,13 +37,6 @@ accumulator)) accumulator)) -; (defun append (list-one list-two) -; (append-tail (reverse list-one) list-two)) - -; (defun append-tail (reversed-list list-two) -; (if (null reversed-list) list-two -; (recur (rest reversed-list) (cons (car reversed-list) list-two)))) - (defun reverse (the-list) (reverse-tail () the-list)) diff --git a/lisp/list/append.lisp b/lisp/list/append.lisp new file mode 100644 index 0000000..7ceba01 --- /dev/null +++ b/lisp/list/append.lisp @@ -0,0 +1,7 @@ + +(defun my-append (list-one list-two) + (append-tail (reverse list-one) list-two)) + +(defun append-tail (reversed-list list-two) + (if (null reversed-list) list-two + (recur (rest reversed-list) (cons (car reversed-list) list-two)))) diff --git a/src/function/builtin/cons/LENGTH.java b/src/function/builtin/cons/LENGTH.java index 2acade0..1046467 100644 --- a/src/function/builtin/cons/LENGTH.java +++ b/src/function/builtin/cons/LENGTH.java @@ -28,16 +28,19 @@ public class LENGTH extends LispFunction { } private ArgumentValidator argumentValidator; + private ArgumentValidator properListValidator; public LENGTH(String name) { this.argumentValidator = new ArgumentValidator(name); this.argumentValidator.setExactNumberOfArguments(1); this.argumentValidator.setEveryArgumentExpectedType(Cons.class); + this.properListValidator = new ArgumentValidator(name + "|list|"); } @Override public LispNumber call(Cons argumentList) { argumentValidator.validate(argumentList); + properListValidator.validate((Cons) argumentList.getFirst()); return callTailRecursive(BigInteger.ZERO, argumentList).invoke(); } diff --git a/test/function/builtin/cons/LENGTHTest.java b/test/function/builtin/cons/LENGTHTest.java index 2968159..f2acc3d 100644 --- a/test/function/builtin/cons/LENGTHTest.java +++ b/test/function/builtin/cons/LENGTHTest.java @@ -7,6 +7,7 @@ import static testutil.TestUtilities.parseString; import org.junit.Test; import function.ArgumentValidator.BadArgumentTypeException; +import function.ArgumentValidator.DottedArgumentListException; import function.ArgumentValidator.TooFewArgumentsException; import function.ArgumentValidator.TooManyArgumentsException; import testutil.SymbolAndFunctionCleaner; @@ -49,4 +50,9 @@ public class LENGTHTest extends SymbolAndFunctionCleaner { evaluateString("(length)"); } + @Test(expected = DottedArgumentListException.class) + public void lengthWithDottedList() { + evaluateString("(length (cons 1 2))"); + } + }