Last active
December 9, 2022 19:15
-
-
Save hauntedhost/a3bf5271bba1369451da to your computer and use it in GitHub Desktop.
guess the number game in clojure, elixir, haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn play [answer lo hi] | |
(let [guess (quot (+ lo hi) 2)] | |
(println (str "Hmm, is it " guess "?")) | |
(cond | |
(< guess answer) (recur answer (inc guess) hi) | |
(> guess answer) (recur answer lo (dec guess)) | |
:else (println (str "Yep! It's " guess "!"))))) | |
; guess.core=> (play 3 1 100000) | |
; Hmm, is it 50000? | |
; Hmm, is it 25000? | |
; Hmm, is it 12500? | |
; Hmm, is it 6250? | |
; Hmm, is it 3125? | |
; Hmm, is it 1562? | |
; Hmm, is it 781? | |
; Hmm, is it 390? | |
; Hmm, is it 195? | |
; Hmm, is it 97? | |
; Hmm, is it 48? | |
; Hmm, is it 24? | |
; Hmm, is it 12? | |
; Hmm, is it 6? | |
; Hmm, is it 3? | |
; Yep! It's 3! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Guess do | |
def play(answer, range = low..high) do | |
guess = div(low + high, 2) | |
IO.puts("Hmm, is it #{guess}?") | |
_play(answer, guess, range) | |
end | |
defp _play(answer, answer, _), | |
do: IO.puts "Yep! It's #{answer}!" | |
defp _play(answer, guess, _low..high) when guess < answer, | |
do: play(answer, (guess + 1)..high) | |
defp _play(answer, guess, low.._high) when guess > answer, | |
do: play(answer, low..(guess - 1)) | |
end | |
# iex(15)> Guess.play(3, 1..100000) | |
# Hmm, is it 50000? | |
# Hmm, is it 25000? | |
# Hmm, is it 12500? | |
# Hmm, is it 6250? | |
# Hmm, is it 3125? | |
# Hmm, is it 1562? | |
# Hmm, is it 781? | |
# Hmm, is it 390? | |
# Hmm, is it 195? | |
# Hmm, is it 97? | |
# Hmm, is it 48? | |
# Hmm, is it 24? | |
# Hmm, is it 12? | |
# Hmm, is it 6? | |
# Hmm, is it 3? | |
# Yep! It's 3! | |
# 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- pure version | |
play answer lo hi | |
| guess < answer = play answer guess hi | |
| guess > answer = play answer lo guess | |
| otherwise = guess | |
where guess = quot (lo + hi) 2 | |
-- io version | |
playio answer lo hi = do let guess = quot (lo + hi) 2 | |
putStrLn("Hmm, is it " ++ show guess ++ "?") | |
case compare guess answer of | |
LT -> do playio answer (guess + 1) hi | |
GT -> do playio answer lo (guess - 1) | |
EQ -> putStrLn("Yep! It's " ++ show guess ++ "!") | |
-- *Main> playio 3 1 100000 | |
-- Hmm, is it 50000? | |
-- Hmm, is it 25000? | |
-- Hmm, is it 12500? | |
-- Hmm, is it 6250? | |
-- Hmm, is it 3125? | |
-- Hmm, is it 1562? | |
-- Hmm, is it 781? | |
-- Hmm, is it 390? | |
-- Hmm, is it 195? | |
-- Hmm, is it 97? | |
-- Hmm, is it 48? | |
-- Hmm, is it 24? | |
-- Hmm, is it 12? | |
-- Hmm, is it 6? | |
-- Hmm, is it 3? | |
-- Yep! It's 3! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment