Handle dotted list in length function
This commit is contained in:
parent
d7f908a01d
commit
76cd2b919a
|
@ -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))
|
||||
|
||||
|
|
|
@ -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))))
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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))");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue