git @ Cat's Eye Technologies Robin / 52ae8b5
Implement, and write tests for, subtraction. catseye 13 years ago
3 changed file(s) with 56 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
3838 > macroP = predP isMacro
3939 > numberP = predP isNumber
4040
41 > robinSubtract env ienv (Pair xexpr (Pair yexpr Null)) cc = do
42 > eval env ienv xexpr (\x ->
43 > case x of
44 > Number xv -> eval env ienv yexpr (\y ->
45 > case y of
46 > Number yv -> cc (Number (xv - yv))
47 > other -> raise ienv (Pair (Symbol "expected-number") other))
48 > other -> raise ienv (Pair (Symbol "expected-number") other))
49 > robinSubtract env ienv other cc = raise ienv (Pair (Symbol "illegal-arguments") other)
50
4151 > robinIf env ienv (Pair test (Pair texpr (Pair fexpr Null))) cc = do
4252 > eval env ienv test (\x ->
4353 > case x of
7787 > ("macro?", macroP),
7888 > ("number?", numberP),
7989 > ("equal?", equalP),
80 > -- ("subtract", subtract),
90 > ("subtract", robinSubtract),
8191 > -- ("divide", divide),
8292 > -- ("sign", sign),
8393 > ("macro", macro),
00 #!/bin/sh
1 ghc Main.lhs -o bin/robin
1 ghc --make Main.lhs -o bin/robin
22 rm -f *.o *.hi Robin/*.o Robin/*.hi
302302 | (robin (0 . 1) (core (0 . 1))
303303 | (number? 6 4))
304304 ? robin: uncaught exception: (illegal-arguments 6 4)
305
306 ### `subtract` ###
307
308 `subtract` evaluates its first argument to a rational number, then
309 evaluates its second argument to a rational number, then evaluates
310 to the difference between the first and second numbers.
311
312 | (robin (0 . 1) (core (0 . 1))
313 | (subtract 6 4))
314 = 2
315
316 | (robin (0 . 1) (core (0 . 1))
317 | (subtract 16/15 1/5))
318 = 13/15
319
320 | (robin (0 . 1) (core (0 . 1))
321 | (subtract 1000 8000))
322 = -7000
323
324 Addition may be accomplished by negating the second argument.
325
326 | (robin (0 . 1) (core (0 . 1))
327 | (subtract 999 (subtract 0 999)))
328 = 1998
329
330 `subtract` expects both of its arguments to be numbers.
331
332 | (robin (0 . 1) (core (0 . 1))
333 | (subtract #f 100))
334 ? robin: uncaught exception: (expected-number . #f)
335
336 | (robin (0 . 1) (core (0 . 1))
337 | (subtract 100 ()))
338 ? robin: uncaught exception: (expected-number)
339
340 `subtract` expects exactly two arguments.
341
342 | (robin (0 . 1) (core (0 . 1))
343 | (subtract 100 200 300))
344 ? robin: uncaught exception: (illegal-arguments 100 200 300)
345
346 | (robin (0 . 1) (core (0 . 1))
347 | (subtract))
348 ? robin: uncaught exception: (illegal-arguments)
305349
306350 ### `eval` ###
307351