Jump to content

Menu: Difference between revisions

956 bytes added ,  9 years ago
Add Nimrod
(Added zkl)
(Add Nimrod)
Line 1,120:
fee fie
</pre>
 
=={{header|Nimrod}}==
{{trans|Python}}
<lang nimrod>import strutils, rdstdin
 
proc menu(xs) =
for i,x in xs: echo " ",i,") ",x
 
proc ok(reply, count): bool =
try:
let n = parseInt(reply)
return 0 <= n and n < count
except: return false
 
proc selector(xs, prompt): string =
if xs.len == 0: return ""
var reply = "-1"
while not ok(reply, xs.len):
menu(xs)
reply = readLineFromStdin(prompt).strip()
return xs[parseInt(reply)]
 
const xs = ["fee fie", "huff and puff", "mirror mirror", "tick tock"]
let item = selector(xs, "Which is from the three pigs: ")
echo "You chose: ", item</lang>
Output:
<pre> 0) fee fie
1) huff and puff
2) mirror mirror
3) tick tock
Which is from the three pigs: foo
0) fee fie
1) huff and puff
2) mirror mirror
3) tick tock
Which is from the three pigs: 4
0) fee fie
1) huff and puff
2) mirror mirror
3) tick tock
Which is from the three pigs: 2
You chose: mirror mirror</pre>
 
=={{header|OCaml}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.