haskell错误函数 [英] haskell error function

查看:99
本文介绍了haskell错误函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数 first ,类型为: Int - > [a] - > (错误([a],[a]))和第二个函数 second 类型: [a] - > [a] - > [a]



我正在尝试使用上述函数创建第三个函数。
这个函数的类型是: [Int] - > [a] - >错误[a]



我已经得到这些类型的解决方法,所以不能更改它们。



这是我试过的:

  last :: [Int]  - > [a]  - >错误[a] 
last(x:xs)list = second(first x list)

您可以将使用错误函数的函数的输出传递给其他函数吗?

解决方案假设错误错误monad ,您可以使用monadic绑定操作符()和 uncurry 函数:

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

uncurry 有两个参数(又名咖喱函数)成对的函数。这意味着 uncurry Q ::([a],[a]) - > [a]



绑定运算符从monad中获取一个值并将其传递给monadic函数。这里我们提取由 F 返回的Error monad的值,并将它传递给 Q ,现在变成了一元函数可以在一对列表上运行,这要归功于 return uncurry


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 :: [Int] -> [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.

这篇关于haskell错误函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆