我正在實現一個使用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