Implement, and write tests for, subtraction.
catseye
13 years ago
38 | 38 |
> macroP = predP isMacro
|
39 | 39 |
> numberP = predP isNumber
|
40 | 40 |
|
|
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 |
|
41 | 51 |
> robinIf env ienv (Pair test (Pair texpr (Pair fexpr Null))) cc = do
|
42 | 52 |
> eval env ienv test (\x ->
|
43 | 53 |
> case x of
|
|
77 | 87 |
> ("macro?", macroP),
|
78 | 88 |
> ("number?", numberP),
|
79 | 89 |
> ("equal?", equalP),
|
80 | |
> -- ("subtract", subtract),
|
|
90 |
> ("subtract", robinSubtract),
|
81 | 91 |
> -- ("divide", divide),
|
82 | 92 |
> -- ("sign", sign),
|
83 | 93 |
> ("macro", macro),
|
0 | 0 |
#!/bin/sh
|
1 | |
ghc Main.lhs -o bin/robin
|
|
1 |
ghc --make Main.lhs -o bin/robin
|
2 | 2 |
rm -f *.o *.hi Robin/*.o Robin/*.hi
|
302 | 302 |
| (robin (0 . 1) (core (0 . 1))
|
303 | 303 |
| (number? 6 4))
|
304 | 304 |
? 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)
|
305 | 349 |
|
306 | 350 |
### `eval` ###
|
307 | 351 |
|