continuing to debug falling rain world program

This commit is contained in:
Danny Yoo 2011-07-20 14:06:52 -04:00
parent 615319f9ac
commit cfd65fc782
5 changed files with 89 additions and 5 deletions

View File

@ -41,10 +41,23 @@
(define-struct world (sky ;; listof drop (define-struct world (sky ;; listof drop
)) ))
(define (my-filter f l)
(cond
[(null? l)
'()]
[(f (car l))
(cons (car l)
(my-filter f (cdr l)))]
[else
(my-filter f (cdr l))]))
;; tick: world -> world ;; tick: world -> world
(define (tick w) (define (tick w)
(make-world (make-world
(filter not-on-floor? (my-filter not-on-floor?
(map drop-descend (cons (random-drop) (world-sky w)))))) (map drop-descend (cons (random-drop) (world-sky w))))))
@ -90,9 +103,20 @@
a-scene)) a-scene))
(define (my-foldl f acc lst)
(cond
[(null? lst)
acc]
[else
(my-foldl f
(f (car (car lst)) acc)
(cdr lst))]))
;; draw: world -> scene ;; draw: world -> scene
(define (draw w) (define (draw w)
(foldl place-drop BACKGROUND (world-sky w))) (my-foldl place-drop BACKGROUND (world-sky w)))

View File

@ -24,6 +24,29 @@
} }
}; };
var makeCheckParameterizedArgumentType = function(parameterizedPredicate,
parameterizedPredicateName) {
return function(MACHINE, callerName, position) {
var args = [];
for (var i = 3; i < arguments.length; i++) {
args.push(arguments[i]);
}
testArgument(
MACHINE,
parameterizedPredicateName.apply(null, args),
function(x) {
return parameterizedPredicate.apply(null, [x].concat(args));
},
MACHINE.env[MACHINE.env.length - 1 - position],
position,
callerName);
return MACHINE.env[MACHINE.env.length - 1 - position];
}
};
var makeCheckListofArgumentType = function(predicate, predicateName) { var makeCheckListofArgumentType = function(predicate, predicateName) {
var listPredicate = function(x) { var listPredicate = function(x) {
@ -127,6 +150,14 @@
plt.baselib.numbers.isNatural, plt.baselib.numbers.isNatural,
'natural'); 'natural');
var checkNaturalInRange = makeCheckParameterizedArgumentType(
function(x, a, b) {
return plt.baselib.numbers.isNatural(x) &&
}
function(a, b) {
return plt.baselib.format('natural between ~a and ~a', [a, b])
});
var checkInteger = makeCheckArgumentType( var checkInteger = makeCheckArgumentType(
plt.baselib.numbers.isInteger, plt.baselib.numbers.isInteger,
'integer'); 'integer');
@ -182,6 +213,7 @@
exports.testArgument = testArgument; exports.testArgument = testArgument;
exports.testArity = testArity; exports.testArity = testArity;
exports.makeCheckArgumentType = makeCheckArgumentType; exports.makeCheckArgumentType = makeCheckArgumentType;
exports.makeCheckParameterizedArgumentType = makeCheckParameterizedArgumentType;
exports.makeCheckListofArgumentType = makeCheckListofArgumentType; exports.makeCheckListofArgumentType = makeCheckListofArgumentType;
exports.checkOutputPort = checkOutputPort; exports.checkOutputPort = checkOutputPort;
@ -192,6 +224,7 @@
exports.checkReal = checkReal; exports.checkReal = checkReal;
exports.checkNonNegativeReal = checkNonNegativeReal; exports.checkNonNegativeReal = checkNonNegativeReal;
exports.checkNatural = checkNatural; exports.checkNatural = checkNatural;
exports.checkNaturalInRange = checkNaturalInRange;
exports.checkInteger = checkInteger; exports.checkInteger = checkInteger;
exports.checkRational = checkRational; exports.checkRational = checkRational;
exports.checkPair = checkPair; exports.checkPair = checkPair;

View File

@ -170,7 +170,7 @@
// Returns true if x is a list (a chain of pairs terminated by EMPTY). // Returns true if x is a list (a chain of pairs terminated by EMPTY).
var isList = function(x) { var isList = function(x) {
while (x !== Empty.EMPTY) { while (x !== Empty.EMPTY) {
if (x instanceof Cons){ if (x instanceof Cons) {
x = x.rest; x = x.rest;
} else { } else {
return false; return false;
@ -191,6 +191,17 @@
}; };
var length = function(lst) {
var len = 0;
while (lst !== EMPTY) {
len++;
lst = lst.rest;
}
return len;
};
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
exports.EMPTY = EMPTY; exports.EMPTY = EMPTY;
@ -202,6 +213,7 @@
exports.makePair = makePair; exports.makePair = makePair;
exports.makeList = makeList; exports.makeList = makeList;
exports.reverse = reverse; exports.reverse = reverse;
exports.length = length;
})(this['plt'].baselib); })(this['plt'].baselib);

View File

@ -115,6 +115,7 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
var checkReal = plt.baselib.check.checkReal; var checkReal = plt.baselib.check.checkReal;
var checkNonNegativeReal = plt.baselib.check.checkNonNegativeReal; var checkNonNegativeReal = plt.baselib.check.checkNonNegativeReal;
var checkNatural = plt.baselib.check.checkNatural; var checkNatural = plt.baselib.check.checkNatural;
var checkNaturalInRange = plt.baselib.check.checkNaturalInRange;
var checkInteger = plt.baselib.check.checkInteger; var checkInteger = plt.baselib.check.checkInteger;
var checkRational = plt.baselib.check.checkRational; var checkRational = plt.baselib.check.checkRational;
var checkPair = plt.baselib.check.checkPair; var checkPair = plt.baselib.check.checkPair;
@ -828,6 +829,19 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
return result; return result;
}); });
installPrimitiveProcedure(
'list-ref',
2,
function(MACHINE) {
var lst = checkList(MACHINE, 'list-ref', 0);
var index = checkNaturalInRange(MACHINE, 'list-ref', 1,
0, plt.baselib.lists.length(lst));
});
installPrimitiveProcedure( installPrimitiveProcedure(
'car', 'car',
1, 1,
@ -951,7 +965,8 @@ if(this['plt'] === undefined) { this['plt'] = {}; }
var elts = checkVector(MACHINE, 'vector-set!', 0).elts; var elts = checkVector(MACHINE, 'vector-set!', 0).elts;
// FIXME: check out-of-bounds vector // FIXME: check out-of-bounds vector
var index = plt.baselib.numbers.toFixnum( var index = plt.baselib.numbers.toFixnum(
checkNatural(MACHINE, 'vector-set!', 1)); checkNaturalInRange(MACHINE, 'vector-set!', 1,
0, elts.length));
var val = MACHINE.env[MACHINE.env.length - 1 - 2]; var val = MACHINE.env[MACHINE.env.length - 1 - 2];
elts[index] = val; elts[index] = val;
return VOID; return VOID;

View File

@ -291,7 +291,7 @@ exact?
length length
;; list? ;; list?
;; list* ;; list*
;; list-ref list-ref
;; list-tail ;; list-tail
append append
reverse reverse