[Haskell-beginners] Re: list of integers to list of nats
Christian Maeder
Christian.Maeder at dfki.de
Mon Feb 22 05:25:19 EST 2010
kane96 at gmx.de schrieb:
> the function should produce Nothing if there is a negative Int in the input list and otherwise Just with the list of the corresponding Nats
>
> mapM looks like the right function for that, so I tried some examples that work like I need it. But in case of my exercise:
>
> mapM toEnum [1,2,3,4] :: Nat
>
> doesn't work.
Surely, it does not work, because the type of your first argument
"toEnum" does not match the expected type "Int -> Maybe Nat", as pointed
out below. Furthermore the overall result type is not "Nat" nor "[Nat]",
but "Maybe [Nat]".
Did you have higher order functions (like mapM or map) in your course?
If not, you are supposed to program "mapIntsToNats" using explizit
recursion over lists (and using case-distinctions aka pattern matching
for Lists and the Maybe data type).
Cheers Christian
P.S.
If you're looking for a ready solution, you're (most likely) wrong here.
We only try to help you, to find a solution yourself.
>> mapM :: Monad m => (a -> m b) -> [a] -> m [b]
>>
>> is also mapM :: (Int -> Maybe Nat) -> [Int] -> Maybe [Nat].
>>
>> so mapIntsToNats = mapM f, for some function f :: Int -> Maybe Nat
You should be able to program such a function "f" (using "toEnum" or
programming it from scratch similar to "toEnum").
More information about the Beginners
mailing list