12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- ;;Here are two macro ideas (the derivative idea is going to be too much work at the moment):
- ;; write a if-not macro that swaps the order of the then and else branches:
- (defmacro (if-not fool yes no)
- `(if ,fool
- ,no
- ,yes))
- (TEST
- > (if-not (< 10 11) "no" "yes")
- "yes")
- ;write a nif macro that takes any number of branches, similar to cond, but without parens around the test and subsequent then branch; the last branch should be the else branch:
- (def (nif-expand args) (if-let-list ))
- (TEST
- > (def (t a b c)
-
- (nif a "a" b "b" c "c" "none"))
- > (t #f #f #f)
- "none"
- > (t #f #f #t)
- "c"
- > (t #t #f #t)
- "a"
- > (t #t #t #t)
- "a"
- > (t #f #t #t)
- "b")
- (defmacro (nif . args)
- `(if-let-list ((,args) (args)))
- "none")
- ;;- write a var macro that takes a string as its argument, which is the name of the variable it should reference.
- (TEST
- > (let ((a 10) (b 20))
- (+ (var "a") (var "b")))
- 30)
- ;; You'll need to use assert* again, as well as string->symbol.
- ;; write a for macro that works similar to Python:
- (TEST
- > (def vals '())
- > (for x '(1 4 5 6) (push! vals (* 3 x)))
- > vals
- (18 15 12 3))
- ;;Note: use the .for-each method, which takes a list or vector as its first argument and a procedure of one argument as its second argument.
|