【发布时间】:2015-10-12 22:55:37
【问题描述】:
我正在实现一个函数myFunction,它使用anotherFunction。
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)) -}
如何做到这一点?
【问题讨论】:
-
使用案例陈述。
-
在 Haskell 普通函数(不是构造函数)中通常以小写字母开头。
-
不仅它们通常以小写开头,而且它们必须以小写开头才能在Haskell中被允许。我相应地编辑了问题。 — —(原则上,下划线也可以作为变量名的第一个字符,但不要这样做——the underscore has a special meaning.