简体   繁体   中英

Error in using Maybe types in CLEAN

i'm a newbie with functional programming and 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.)

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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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