0

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!!

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Thomas
  • 366
  • 6
  • 19
  • Seems like you need to enclose `Just[xs]` in parenthesis in the last line. – max taldykin Oct 14 '14 at 08:25
  • yes, i tried, and it fails, what i dont understand. Type error [Prac.icl,32,sequence]:"argument 1 of list constructor" cannot unify types: v9 [Maybe v9] – Thomas Oct 14 '14 at 08:38
  • yes, combine works fine. combine (Just 1) (Just [2, 3]) == Just [1, 2, 3] – Thomas Oct 14 '14 at 08:52
  • If this works then I am not sure about the syntax of CLEAN - I answered with something that will work in Haskell - maybe you can try it – Random Dev Oct 14 '14 at 08:55

1 Answers1

5

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

Random Dev
  • 51,810
  • 9
  • 92
  • 119
  • "Run time error, rule 'sequence' in module does not match" – Thomas Oct 14 '14 at 09:29
  • @GaborToth I cannot help here to much - in Haskell there is a `sequence` in the prelude you have to hide `import Prelude hiding (sequence)` or you have to give another name to it - no clue if this is different in CLEAN - or maybe you have to give the compiler some clue that this is a recursive function (for example you have to do so in F#) - or a combination of these two – Random Dev Oct 14 '14 at 09:41
  • @GaborToth based on the Wiki page it's not the recursion so I would first try to give it another name in case there too is a sequence in your prelude or StdEnv or whatever it's called – Random Dev Oct 14 '14 at 09:46
  • Thank you Carsten for your time, its really good! (I missed in the combine's definition one thing!) – Thomas Oct 14 '14 at 10:13
  • @GaborToth no problem - glad I could help - and now I learned about clean too ;) – Random Dev Oct 14 '14 at 10:47
  • In Clean, you can also simply `import StdMaybe` (you have to add `$CLEANHOME/lib/StdLib` to the include path) to get the `Maybe` type along with `isNothing`, `fromJust` and possibly other functions. –  Oct 06 '15 at 18:57