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))"); + } + }