簡體   English   中英

在CLEAN中使用Maybe類型時出錯

[英]Error in using Maybe types in CLEAN

我是函數式編程和CLEAN的新手。 我有幾個功能,但一次卻出錯,我不知道為什么。 (我用Haskell標記了它,因為它與CLEAN非常相似。)

我的模塊:

module Prac

combine :: (Maybe a) (Maybe [a]) -> Maybe [a]
combine Nothing _ = Nothing
combine _ Nothing = Nothing
combine (Just d) (Just [x:xs]) = Just [d, x:xs]

sequence :: [Maybe a] -> Maybe [a]
sequence [Just x:xs] =  combine  (Just x)  Just[xs]

它在序列定義處失敗:

 Type error [Prac.icl,32,sequence]: near combine : cannot unify types:
 [v8] -> Maybe [v4]
 Maybe [v5]

非常感謝!!

這將在Haskell中起作用:

combine :: Maybe a -> Maybe [a] -> Maybe [a]
combine Nothing _ = Nothing
combine _ Nothing = Nothing
combine (Just d) (Just xs) = Just (d:xs)

sequence :: [Maybe a] -> Maybe [a]
sequence [] = Just []
sequence (a:xs) =  combine  a (sequence xs)

但我不確定它是否滿足您的要求:

λ> sequence [Just 1, Just 3, Just 4]
Just [1,3,4]

λ> sequence [Just 1, Nothing, Just 4]
Nothing

λ> sequence []
Just []

好的,我找到了翻譯方案 -但是沒有保證,因為我現在沒有辦法測試其正確性

sequence :: [Maybe a] -> Maybe [a]
sequence [] = Just []
sequence [x:xs] =  combine x (sequence xs)

雖然不確定空白清單和簽名-對不起

無論如何,如果您可以重用只是將給定列表的頭部與尾部的遞歸計算序列相結合的想法,那應該很好

這對我有用干凈

因此,我剛剛下載了IDE,創建了一個項目,並添加了一個模塊:

module seq

:: MayBe a = Just a | Nothing

Start = sequence [Just 3, Just 4, Just 5]

combine Nothing _ = Nothing
combine _ Nothing = Nothing
combine (Just d) (Just xs) = Just [d:xs]

sequence [] = Just []
sequence [x:xs] =  combine x (sequence xs)

編譯它,更新項目並運行它-在這里工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM