2

I have a sign function, which can return an error.

signe :: Int -> Char
signe chiffre
    | chiffre >= 1 && chiffre <= 9 = '+'
    | chiffre == 0 = '0'
    | chiffre >= -9 && chiffre <= (-1) = '-'
    | otherwise = error "Erreur in the sign"

I'd like to make a simple one to return the corresponding code of the sign, but with error handling.

signes liste = [ signe x | x<-liste ]

I give you an example : For now, if I call

signes [1,3,0,-10]

it gives me

++0*** Exception: Error in sign.

I'd like to have nothing instead of Exception: ++0.

1
  • 1
    I wanted to start my message by "hi everyone" but even with editing it's not taking in account ... Commented Nov 14, 2013 at 17:57

3 Answers 3

3

You can, and should, use Maybe in such cases:

signe chiffre 
   | chiffre >= 1 && chiffre <= 9 = Just '+'
   ....
   | otherwise = Nothing    -- parbleu!!

signes = mapMaybe signe

You may need to import Data.Maybe for the mapMaybe function.

Sign up to request clarification or add additional context in comments.

6 Comments

mapMaybes --> "Not in scope". (I've added at the top of my sheet "import Data.List") So i just tryed signes = map signe But i get [Just '+' , ........, Nothing ]
It's actually just mapMaybe. See the docs.
Thank you for pointing it out, @AlexReinking , it is actually mapMaybe and it is in Data.Maybe. Always confuse that with catMaybes where there is an s at the end. COrrected above.
I had a look on the docs and i tried mapMaybe in case too, before answering. Same problem ... Here are my functions : import Data.List signe chiffre | chiffre >= 1 && chiffre <= 9 = Just '+' | chiffre == 0 = Just '0' | chiffre >= -9 && chiffre <= (-1) = Just '-' | otherwise = Nothing signes = mapMaybe signe And i'm sorry to write here but "Users with less than 10 reputation can't answer their own question for 8 hours after asking. You can answer 11/15/2013 1:56:02 AM. Until then please use comments, or edit your question instead." ...
Ingo thanks it worked !! Thanks to all of you :) !! Another Haskell question is : Would it be possible to create a SIMPLE code to know if a list is increasing ? Maybe with foldr or another thing but to make something simple ?
|
2

A better way would be to actually use the Maybe type which lets you literally return Nothing or Just aValue. You could rewrite your function as

signe :: Int -> Maybe Char
signe chiffre
    | chiffre >= 1 && chiffre <= 9 = Just '+'
    | chiffre == 0 = Just '0'
    | chiffre >= (-9) && chiffre <= (-1) = Just '-'
    | otherwise = Nothing

Comments

0

The problem already seems to be answered by Ingo, but I wanted to point out that since you had an error message in the original question, perhaps "Either" would be a better choice here

signe :: Int -> Either String Char
signe chiffre
    | chiffre >= 1 && chiffre <= 9 = Right'+'
    | chiffre == 0 = Right '0'
    | chiffre >= -9 && chiffre <= (-1) = Right '-'
    | otherwise = Left "Erreur in the sign"

where you can get the filtered list with

signes liste = [ x | Right x<-map signe liste ]

Both Maybe and Either are used for error checking, Either gives you the ability to pass an "Exception" up the call chain.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.