## Guessing Game

```#!/usr/bin/env mars

# Guessing Game

import prelude

# Result of asking user for input
type Guess:
Higher
Lower
Equal

# Presents a guess and asks the user whether it's higher, lower, or equal.
def ask(num :: Num) :: io Guess:
var l :: Array(Num)
print_string("My guess: ")
print_value(num)
print_string(". Higher (h), lower (l) or equal (e)? ")
l = get_line()
if l == "h":
return Higher
elif l == "l":
return Lower
elif l == "e":
return Equal
else:
print_string("Please enter h, l or e.\n")

# Given a range, makes a guess
def guess(min :: Num, max :: Num) :: Num:
return (max - min) // 2 + min

# Plays the game until success
def guessing_loop(min :: Num, max :: Num) :: io Num:
var myguess :: Num
var result :: Guess
if min > max:
print_string("You seem a bit confused. Let's start again.\n")
min = 0
max = 100
myguess = guess(min, max)
switch result:
case Equal:
print_string("I win!\n")
return 0
case Lower:
max = myguess - 1
case Higher:
min = myguess + 1
return guessing_loop(min, max)

def main() :: io Num:
print_string("Think of a number [0,100].\n")
return guessing_loop(0, 100)
```