1 minute read

Code kata


The elevator can be on first floor or second floor The elevator can be either openned or closed. The elevator can go up or down. But when it goes up or down, the door has to be closed. The door can open or close, but it cannot open when it is already openned or close when it is already closed.

Write a function that takes a list of actions with :done indicating the end, and return if this sequence is legal or not.

This example comes from The Joy of Clojure 2nd.


(defn elevator [commands]
  (letfn [(ff-open [[_ & r]]
            #(case _
               :close (ff-close r)
               :done true
          (ff-close [[_ & r]]
            #(case _
               :open (ff-open r)
               :up (sf-close r)
          (sf-close [[_ & r]]
            #(case _
               :open (sf-open r)
               :down (ff-close r)
          (sf-open [[_ & r]]
            #(case _
               :close (sf-close r)
               :done true
    (trampoline ff-open commands)))
comments powered by Disqus