Menu: Difference between revisions

Content added Content deleted
No edit summary
(→‎{{header|Clojure}}: fix verification and return type; add example invocation)
Line 156: Line 156:


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang lisp>(defn menu [strs]
<lang clojure>(defn menu [prompt choices]
(if (empty? strs)
(if (empty? choices)
""
""
(let [menustr
(let [menutxt (apply str (interleave
(apply str
(iterate inc 1)
(interleave (iterate #(+ 1 %) 1)
(map #(str \space % \newline) choices)))]
(println menutxt)
(map #(apply str (conj (seq (concat (seq %) "\n"))
(print prompt)
\space)) strs)))]
(println menustr)
(print "Please enter a number: ")
(flush)
(flush)
(let [len (count strs)
(let [index (read-string (read-line))]
; verify
input (read-string (read-line))]
(if (or (> input len) (< input len) (not (integer? input)))
(if (or (not (integer? index))
(recur strs)
(> index (count choices))
(println (nth strs (- input 1))))))))</lang>
(< index 1))
; try again
(recur prompt choices)
; ok
(nth choices (dec index)))))))

(println "You chose: "
(menu "Which is from the three pigs: "
["fee fie" "huff and puff" "mirror mirror" "tick tock"]))</lang>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==