简体   繁体   English

在CLEAN中使用Maybe类型时出错

[英]Error in using Maybe types in CLEAN

i'm a newbie with functional programming and CLEAN. 我是函数式编程和CLEAN的新手。 I have a few functions and i get error in one, and i cannot figured out why. 我有几个功能,但一次却出错,我不知道为什么。 (I tagged that with Haskell because it's very similar to CLEAN.) (我用Haskell标记了它,因为它与CLEAN非常相似。)

My module: 我的模块:

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]

It fails at the sequence definition: 它在序列定义处失败:

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

Many Thanks!! 非常感谢!!

This will work in Haskell: 这将在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)

but I'm not sure if it does what you want: 但我不确定它是否满足您的要求:

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

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

λ> sequence []
Just []

ok, I have found a translation scheme - but no gurantee as I don't have a way to test it rigth now 好的,我找到了翻译方案 -但是没有保证,因为我现在没有办法测试其正确性

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

not sure about the empty list and the signature though - sorry 虽然不确定空白清单和签名-对不起

Anyway if you can reuse the idea that you just combine the head of the given list with the recursive computated sequence of the tail you should be fine 无论如何,如果您可以重用只是将给定列表的头部与尾部的递归计算序列相结合的想法,那应该很好

this works for me using clean 这对我有用干净

So I just downloaded the IDE, made a project, added one module: 因此,我刚刚下载了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)

compiled this, updated the project and did run it - and here this works 编译它,更新项目并运行它-在这里工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM