Fork me on GitHub
#clojure-uk
<
2020-04-24
>
dharrigan06:04:21

Good Morning!

guy07:04:43

Morning!

dharrigan08:04:09

So, today, continuing on with my little introduction to some other colleagues at work (non-programmers) on Clojure

dharrigan08:04:57

today I'l be introducing if and cond and looping by way of a simple guessing game (computer chooses a random number, and you get to type a number in and it tells you if it's higher or lower, until you guess right!)

dharrigan08:04:14

Bit like Bruce's Play Your Cards Right 🙂

♠️ 8
mccraigmccraig08:04:51

looping as in loop/`recur` @dharrigan?

mccraigmccraig10:04:46

is that because loop/`recur` make for conceptually easy console-based input cycles ?

dharrigan10:04:56

I think it's easy just to say here is a form for doing a loop

dharrigan10:04:14

but don't go too much into the fact of how it works too much.

dharrigan10:04:51

(defn guessing-game
  []
  (let [my-number (rand-int 100)]
    (loop [guess (read-number)]
      (if (= guess my-number)
        (println "Well Done! Yes, the number I was thinking of was:" my-number)
        (do
         (cond
          (> guess my-number) (println "My number is lower!")
          (< guess my-number) (println "My number is higher!"))
         (recur (read-number)))))))

dharrigan10:04:55

it's very simple

dharrigan10:04:26

if I even manage to get them to do 1/3 of that, then I'm on a winner 🙂

mccraigmccraig11:04:06

yeah, i was just curious, 'cos i actively avoid loop/`recur` most of the time, but for simple prompt stuff it is conceptually very straightforward

dharrigan11:04:28

yeah, further explanations of recur are for another time....

folcon15:04:23

Same, it’s interesting I think what you need to tailor based on whether your audience have programmed before or not…