我正在实现一个使用anotherFunction
的函数myFunction
。获取Haskell中Maybe的值
anotherFunction
是一个无法修改的外部函数。它返回一个Maybe
类型的值。
myFunction
是递归函数,用于检查另一个myFunction
返回的值是Just
值还是Nothing
。如果是Nothing
则返回Nothing
,否则将使用myFunction
返回的纯值作为参数anotherFunction
。
基本上是这样的:
--These cannot be modified
data A = B | F a
anotherFunction :: x -> Maybe x
--Something here
myFunction :: A -> Maybe x
--These can be modified
myFunction (F a) = {- if (myFunction a == Nothing)
then Nothing
else anotherFunction (pure value of (myFunction a)) -}
如何才能实现这一目标?
使用case语句。在Haskell中正常函数(不是构造函数)中的 – user1937198
通常以小写字母开头。 – jakubdaniel
不仅____它们通常以小写字母开头,它们必须以小写字母开头才能在Haskell中允许。我相应地编辑了这个问题。 - - (原则上,下划线也可以作为变量名的第一个字符,但不要这样做 - [下划线有特殊含义](http://stackoverflow.com/questions/21282515/is- – leftaroundabout