Re: [Haskell-beginners] list of integers to list of nats

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. -------- Original-Nachricht --------
Datum: Sat, 20 Feb 2010 13:19:32 +0100 Von: "Jonas Almström Duregård"
An: kane96@gmx.de Betreff: Re: [Haskell-beginners] list of integers to list of nats
Homework?
Should mapIntsToNats [-1] be Nothing?
One way to to do this is to check if any integer in the list is negative (for instance using the any function) and if so return Nothing, otherwise Just map an Int-to-Nat function across the values in the list (there is already such a function in your code).
A slightly more elegant solution uses the fact that Maybe is a Monad, so the function
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
Hope this helps /Jonas Duregård
Hi, I have to write a function which maps a list of integers to a list of
On 20 February 2010 12:39,
wrote: the corresponding nats. The following code is already there: data Nat = Z | S Nat deriving (Eq,Ord,Show)
instance Enum Nat where toEnum i | i < 0 = error "Enum_Nat.toEnum: Negative
integer"
| i == 0 = Z | otherwise = S (toEnum (i-1))
the function should be: mapIntsToNats :: [Int] -> Maybe [Nat] so for example: [2,0,1,3] should make: [S (S Z), Z, S Z, S (S (S Z))]
how can I do that?
-- NEU: Mit GMX DSL über 1000,- ¿ sparen! http://portal.gmx.net/de/go/dsl02 _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- NEU: Mit GMX DSL über 1000,- ¿ sparen! http://portal.gmx.net/de/go/dsl02

I tried: map toEnum [1,2,3,4]) :: [Nat] which works normally, but not for Maybe in: mapIntsToNats :: [Int] -> Maybe [Nat] -------- Original-Nachricht --------
Datum: Sat, 20 Feb 2010 19:18:57 +0100 Von: kane96@gmx.de An: jonas.duregard@gmail.com CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] list of integers to list of nats
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.
-------- Original-Nachricht --------
Datum: Sat, 20 Feb 2010 13:19:32 +0100 Von: "Jonas Almström Duregård"
An: kane96@gmx.de Betreff: Re: [Haskell-beginners] list of integers to list of nats Homework?
Should mapIntsToNats [-1] be Nothing?
One way to to do this is to check if any integer in the list is negative (for instance using the any function) and if so return Nothing, otherwise Just map an Int-to-Nat function across the values in the list (there is already such a function in your code).
A slightly more elegant solution uses the fact that Maybe is a Monad, so the function
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
Hope this helps /Jonas Duregård
Hi, I have to write a function which maps a list of integers to a list of
On 20 February 2010 12:39,
wrote: the corresponding nats. The following code is already there: data Nat = Z | S Nat deriving (Eq,Ord,Show)
instance Enum Nat where toEnum i | i < 0 = error "Enum_Nat.toEnum: Negative
integer"
| i == 0 = Z | otherwise = S (toEnum (i-1))
the function should be: mapIntsToNats :: [Int] -> Maybe [Nat] so for example: [2,0,1,3] should make: [S (S Z), Z, S Z, S (S (S Z))]
how can I do that?
-- NEU: Mit GMX DSL über 1000,- ¿ sparen! http://portal.gmx.net/de/go/dsl02 _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- NEU: Mit GMX DSL über 1000,- ¿ sparen! http://portal.gmx.net/de/go/dsl02 _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT! Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

Am Sonntag 21 Februar 2010 15:57:09 schrieb kane96@gmx.de:
I tried: map toEnum [1,2,3,4]) :: [Nat] which works normally, but not for Maybe in: mapIntsToNats :: [Int] -> Maybe [Nat]
To use mapM, you need a function of type (f :: Int -> Maybe Nat). toEnum has type (Int -> Nat), so you can't use that directly. Think: when should f k be Nothing and what should f k be in the other cases?

f should be Nothing if k is negative and otherwise k should be toEnum k...?! -------- Original-Nachricht --------
Datum: Sun, 21 Feb 2010 16:28:58 +0100 Von: Daniel Fischer
An: beginners@haskell.org CC: kane96@gmx.de Betreff: Re: [Haskell-beginners] list of integers to list of nats
Am Sonntag 21 Februar 2010 15:57:09 schrieb kane96@gmx.de:
I tried: map toEnum [1,2,3,4]) :: [Nat] which works normally, but not for Maybe in: mapIntsToNats :: [Int] -> Maybe [Nat]
To use mapM, you need a function of type (f :: Int -> Maybe Nat). toEnum has type (Int -> Nat), so you can't use that directly. Think: when should f k be Nothing and what should f k be in the other cases?
-- Sicherer, schneller und einfacher. Die aktuellen Internet-Browser - jetzt kostenlos herunterladen! http://portal.gmx.net/de/go/atbrowser

kane96@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").
participants (3)
-
Christian Maeder
-
Daniel Fischer
-
kane96@gmx.de