Handle dotted list in length function
This commit is contained in:
parent
d7f908a01d
commit
76cd2b919a
|
@ -37,13 +37,6 @@
|
||||||
accumulator))
|
accumulator))
|
||||||
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)
|
(defun reverse (the-list)
|
||||||
(reverse-tail () 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 argumentValidator;
|
||||||
|
private ArgumentValidator properListValidator;
|
||||||
|
|
||||||
public LENGTH(String name) {
|
public LENGTH(String name) {
|
||||||
this.argumentValidator = new ArgumentValidator(name);
|
this.argumentValidator = new ArgumentValidator(name);
|
||||||
this.argumentValidator.setExactNumberOfArguments(1);
|
this.argumentValidator.setExactNumberOfArguments(1);
|
||||||
this.argumentValidator.setEveryArgumentExpectedType(Cons.class);
|
this.argumentValidator.setEveryArgumentExpectedType(Cons.class);
|
||||||
|
this.properListValidator = new ArgumentValidator(name + "|list|");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LispNumber call(Cons argumentList) {
|
public LispNumber call(Cons argumentList) {
|
||||||
argumentValidator.validate(argumentList);
|
argumentValidator.validate(argumentList);
|
||||||
|
properListValidator.validate((Cons) argumentList.getFirst());
|
||||||
|
|
||||||
return callTailRecursive(BigInteger.ZERO, argumentList).invoke();
|
return callTailRecursive(BigInteger.ZERO, argumentList).invoke();
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import static testutil.TestUtilities.parseString;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import function.ArgumentValidator.BadArgumentTypeException;
|
import function.ArgumentValidator.BadArgumentTypeException;
|
||||||
|
import function.ArgumentValidator.DottedArgumentListException;
|
||||||
import function.ArgumentValidator.TooFewArgumentsException;
|
import function.ArgumentValidator.TooFewArgumentsException;
|
||||||
import function.ArgumentValidator.TooManyArgumentsException;
|
import function.ArgumentValidator.TooManyArgumentsException;
|
||||||
import testutil.SymbolAndFunctionCleaner;
|
import testutil.SymbolAndFunctionCleaner;
|
||||||
|
@ -49,4 +50,9 @@ public class LENGTHTest extends SymbolAndFunctionCleaner {
|
||||||
evaluateString("(length)");
|
evaluateString("(length)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = DottedArgumentListException.class)
|
||||||
|
public void lengthWithDottedList() {
|
||||||
|
evaluateString("(length (cons 1 2))");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue