   "flatMap for exercise 6.8 from FPINS but generalized to state for exercise 6.10"
   [f g]
   #(from-state (f %) :let [v s]
-               ((g v) s)))
+              ((g v) s)))
 
 (defn unit-state
   "Generalized Unit function from section 6.5 of fpins"
   [a]
-  (fn [rng] (make-state a rng)))
+  (fn [s] (make-state a s)))
 
 (defn map-via-flat-map-s
   "Generalized version of map-via-flat-map from exercise 6.9, for exercise 6.10"

  "inputs is sequence of either coins or turns, s is 'machine' in focus"
  [i]
  (fn [s]
    (println s)
    (match [i s]
         [_ {:locked _ :candies 0 :coins _}] s
         [:coin {:locked false :candies _ :coins _}] s
         [:turn {:locked true :candies _ :coins _}] s
         [:coin {:locked true :candies candies :coins coins}]
         (machine false candies (inc coins))
         [:turn {:locked false :candies candies :coins coins}]
         (machine true (dec candies) coins))))

    (unit-state '())                                        ;Check out that "unit-produced" function is used as z value (now - it wouldn't be really
    #(map2-via-flat-map-s % %2 (fn [a b] (cons a b)))       ;correct to say it is "initial" but rather neutral (or terminating?) element
    fs))                                                    ;UPDATE (25.11.2015) Check out wikipedia about monad, it is actually "return" also
                                                            ;called "unit" operation of a monad

(declare simple-rng-s)

