繁体   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