1

the goal is to Write a function that, given an integer between 1 and 12 evaluates to the name of the related month (1->January, etc.) Else, it may evaluate to "Incorrect month number". This is my code:

list = ["Jan","Feb","Mar","April", "May", "Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
month x = if x<13 && x>0
          then x = list !! (x-1)
          else "Not a month"

this is the error msg: parse error on input "="

Many thanks for your help

2
  • @YassineBotabia: changing it to || would in fact change the semantics and generate errors. Furthemore the error is not with the x = on the first line, but the one on the second line. Commented Jan 21, 2017 at 11:46
  • I realized shortly after puting. that's true. Commented Jan 21, 2017 at 12:17

4 Answers 4

4

Just remove x =. (Why did you write that?)

list = ["Jan","Feb","Mar","April","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
month x = if x<13 && x>0
          then list !! (x-1)
          else "Not a month"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you so much for your help.
3

Using guards is the best strategy here:

month x | x > 0 && x < 13      = list !! (x - 1)
        |otherwise             = "Error"

You could also use Maybe for this type of operation:

mylist = ["Jan","Feb","Mar","April", "May", "Jun","Jul","Aug","Sep","Oct","Nov","Dec"]

exampleFunc :: [String] -> Int -> Maybe String
exampleFunc li int
   | int > 0 && int < 13    = Just (li !! (int - 1))
   | otherwise              = Nothing

Using Maybe allows you to return a standard value (Nothing) in the event of an otherwise condition.

2 Comments

Thank you Barbra, you comments are very helpful .
Maybe String should absolutely be used here, since "Not a month" is not, in fact, a month.
2

Well you write:

month x = if x0
          then x = list !! (x-1)
          else "Not a month"

You probably are used to the imperative world (C++, Java,...) where there are variables that you can set and set to other values. In a functional and by extent declarative language, a variable is only set once.

Furthermore there is no need to set x to a value, you only have to return list!!(x-1) so:

month x = if x<13 && x>0
          then list !! (x-1)
          else "Not a month"

should work.

An - in my opinion - more elegant way to write this however is using guards:

month x | x > 0 && x < 13 = list !! (x-1)
        | otherwise = "not a month"

1 Comment

Thank you so much. x= was my error. and you are right I just started functional programming.
1

You can also write it like this:

month = ["Jan","Feb","Mar","April", "May", "Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
getMonth k = lookup k $ zip [1..] month

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.