Arc Forumnew | comments | leaders | submitlogin
Monadic-like macro
6 points by applepie 5971 days ago | 5 comments

  (mac w/ (val . forms)
    ((afn (x f)
       (if (null? f)
          x
          (self `(let _ ,x ,@(car f)) (cdr f))))
     val forms))
__ Example

  (w/ '()
    (cons 10 _)
    (cons 20 _)
    (cons 30 _)
    (map [(* _ 2)] _)
    (list (length _) _))
__

  (3 (20 40 60))


4 points by sjs 5971 days ago | link

Interesting. I might be able to use this.

  (mac w/ (val . forms)
      ((afn (x f)
         (if (no f)
            x
            (self `(let _ ,x ,(car f)) (cdr f))))
       val forms))

-----

1 point by applepie 5971 days ago | link

Sorry about the bugs. I transcribed it from paper :$

-----

2 points by dfranke 5970 days ago | link

This looks like perl's $_, but I don't see what it has to do with monads. What corresponds to >>= and return?

-----

1 point by vrk 5970 days ago | link

Definitely $_, although the initial value has to be defined here explicitly.

-----

2 points by applepie 5970 days ago | link

Now it behaves as Haskell's do-notation for the identity monad.

It could be adapted for other monads if it also took return and (>>=) as arguments.

  (mac w/ (monad x . rest)
    (w/uniq (gmonad)
      ((afn (x rest)
         (if (no rest)
           `(let ,gmonad ,monad ,x)
           (self `((car ,gmonad) (fn (_) ,(car rest)) ,x)
                 (cdr rest))))
       `((cdr ,gmonad) ,x))))

  (= list-ret list)
  (= list-bind mappend)

  (= list-monad (cons list-bind list-ret))

  (w/ list-monad 10
     (list (- _ 1) _ (+ _ 1))
     (list _ (* _ 2) (* _ 3)))

  ===>
  (9 18 27 10 20 30 11 22 33)

-----