我是功能编程和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]
非常感谢!!
答案 0 :(得分:5)
这适用于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 []
好吧,我找到了一个translation scheme - 但是没有保证,因为我现在没有办法对它进行测试
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)
编译了这个,更新了项目并运行了它 - 这里有效