Skip to content

Instantly share code, notes, and snippets.

@jjcomer
Created July 30, 2011 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jjcomer/1115436 to your computer and use it in GitHub Desktop.
Save jjcomer/1115436 to your computer and use it in GitHub Desktop.
Clojure State Machine
(ns stateMachine)
(defn parse-integer [str]
(try (Integer/parseInt str)
(catch NumberFormatException nfe 0)))
(defn displayMenu
"This function displays the menu and gets the user's input"
[]
(println "Displaying the menu")
(println "Choose your option")
(case (parse-integer (read-line))
1 :option1
2 :option2
3 :option3
:error))
(defn programLoop
"This fuction represents the main program loop"
[]
(letfn [
(startState []
#(getUserChoice))
(getUserChoice []
(case (displayMenu)
:option1 #(option1)
:option2 #(option2)
:option3 #(option3)
#(getUserChoice)))
(option1 []
(println "Option 1")
#(startState))
(option2 []
(println "Option 2")
#(startState))
(option3 []
(println "Option 3")
nil)]
(trampoline startState)))
(programLoop)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment