This is my attempt to grok monadic functions after watching this.
h uses bind to compose together two arbitrary functions f and g. What is the unit operator in this case?
;; f :: int -> [str]
;; g :: str -> [keyword]
;; bind :: [str] -> (str -> [keyword]) -> [keyword]
;; h :: int -> [keyword]
(defn f [v]
(map str (range v)))
(defn g [s]
(map keyword (repeat 4 s)))
(defn bind [l f]
(flatten
(map f l)))
(f 8) ;; :: (0 1 2 3 4 5 6 7)
(g "s") ;; :: (:s :s :s :s)
(defn h [v]
(bind (f v) g))
(h 9)
;; :: (:0 :0 :0 :0 :1 :1 :1 :1 :2 :2 :2 :2 :3 :3 :3 :3 :4 :4 :4 :4 :5 :5 :5 :5)
Ah, thanks for the comments; I see where I was confused.
I was familiar with these functions and how to compose them using bind:
f0 :: a -> M a
g0 :: a -> M a
but not with these functions:
f1 :: a -> M b
g1 :: b -> M c
but essentially, the bind operator is the same for both cases if M is the same. In my case, M is the list monad so f1 and g1 can be combined the same way as f0 and g0.
Are you trying to implement the list monad? If so, then it would be:
This is based on the Haskell implementation: