Post by quintus on Apr 28, 2018 17:51:43 GMT
Hello, I'm currently solving the problems in the LYAH book in Lux as an exercise. As a matter of fact though, I'm stuck on the RPN reader.
Here it is in Hs: learnyouahaskell.com/functionally-solving-problems
In Lux:
I hope someone can help me understand how to solve this pickle: inside folding-func, I need to convert from Text to number, so I'm using Codec<Text,Real>.
The thing is, decode doesn't return a Real but a Error<Real>, and I have no idea how to get out of this... I've looked in the Lux stdlib and I found a function that looks like what I need:
But apparently I can't import it?
I also tried this:
So, uhm, how do I do this?
In case there is an embarrassing mistake I have to admit that I'm still not that comfortable with Monad and Applicative in Lux...
Here it is in Hs: learnyouahaskell.com/functionally-solving-problems
In Lux:
(;module: {#;doc "DOC"}
lux
lux/type/auto
(lux (codata io)
(control monad
monoid
functor
applicative
ord
eq
codec)
(data [text Eq<Text> "Text/" Monoid<Text>])
(data [sum])
(data ["E" error])
(data [number Codec<Text,Real>])
(data/struct [list #* Fold<List> Monoid<List>])
[cli #+ program:])
utils)
(def: (solve-RPN expr)
(All [a] (-> Text (Maybe (Number a))))
(|> expr
(utils;words)
(fold folding-func #;Nil)
(head)))
(def: (folding-func stack item)
(All [a] (-> (List Real) Text (List Real)))
(case stack
(^ (list x y ys))
(let [x_ (E;assume (decode x))
y_ (E;assume (decode y))]
(case item
"+" (#;Cons (+ x_ y_) ys)
"-" (#;Cons (- x_ y_) ys)
"*" (#;Cons (* x_ y_) ys)
_ (list& (%r _) stack)))
_ (list& (%r) item)))
I hope someone can help me understand how to solve this pickle: inside folding-func, I need to convert from Text to number, so I'm using Codec<Text,Real>.
The thing is, decode doesn't return a Real but a Error<Real>, and I have no idea how to get out of this... I've looked in the Lux stdlib and I found a function that looks like what I need:
(def: #export (assume error)
(All [a] (-> (Error a) a))
(case error
(#Success value)
value
(#Error message)
(error! message)))
But apparently I can't import it?
I also tried this:
"+" (do Monad<Error>
[x_ (|> x decode)
y_ (|> y decode)
(wrap (#;Cons (+ x y)))])
So, uhm, how do I do this?
In case there is an embarrassing mistake I have to admit that I'm still not that comfortable with Monad and Applicative in Lux...