开发者

haskell error function

I have one function first with type: Int -> [a] -> (Error ([a],[a])) and a second function second with type: [a] -> [a] -> [a]

I am trying to make a third function now that uses the above functions. the type I have for this function is: [Int] -> [a] -> Error [a]

I have been given these types to work around so cant change them.

This is what I tried:

last :: [I开发者_如何学编程nt] -> [a] -> Error [a]
last (x:xs) list = second (first x list)

Can you pass outputs from functions that use the error function in to others?


Assuming Error is an error monad, you can use the monadic bind operator (>>=) and the uncurry function:

z (x:xs) list = F x list >>= return . uncurry Q

uncurry transforms Q from a function with two arguments (aka a curried function) into a function on pairs. This means that uncurry Q :: ([a], [a]) -> [a]

The bind operator takes a value out of a monad and passes it into a monadic function. Here we're extracting the value of the Error monad returned by F and passing it to Q, now turned into a monadic function that works on a pair of lists, thanks to return and uncurry.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜