Skip to content

Instantly share code, notes, and snippets.

@tuturto
Created February 7, 2017 04:34
Show Gist options
  • Save tuturto/5c9d8fa6e01f0fd37d91deb5548bc5c4 to your computer and use it in GitHub Desktop.
Save tuturto/5c9d8fa6e01f0fd37d91deb5548bc5c4 to your computer and use it in GitHub Desktop.
on page 34 of Evidence-Based programming language design
x <- rand(25);
y <- rand(25);
comment set x and y to random integers in [1, 25];
write What is <(x)> + <(y)>;
[accept reply; if reply
| = x+y : write Very good; correct_count <- correct_count + 1;
| = x*y : write Add, don’t multiply; again;
| > x+y+10 : write No, that’s more than 10 too much; again;
| else : write No, try again; again;
];
@gilch
Copy link

gilch commented Feb 7, 2017

A rough attempt in Hy.

(defmacro kail [&rest body] 
  `(while True ~@body (break)))
(import [random [randint]])
;; set x and y to random integers in [1, 25]
(def
  x (randint 1 25)
  y (randint 1 25)
  correct-count 0)  ; not in example?
(print (apply .format ["What is {x} + {y}"] (vars)))
(kail
  (setv reply (int (input)))
  (cond
    [(= reply (+ x y)) (print "Very good") (+= correct-count 1)]
    [(= reply (* x y)) (print "Add, don't multiply") (continue)]
    [(> reply (+ x y 10)) (print "No, that's more than 10 too much") (continue)]
    [:else (print "No, try again") (continue)]))

I'm mostly doing this to try to understand how exactly KAIL is supposed to work. I noticed that my version repeats reply, but the original doesn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment