user2922645
user2922645

Reputation:

Input error on " = " Haskell

the following code refuses to compile. Please be gracious , I have been working with Haskell for 1.5 weeks. So just a beginner.

                       name = (mod ( tag + x + (div ( 31 * m )  12 )) 7 )  

I have parse error on input "=" It regards to the following line:

name = (mod ( tag + x + (div ( 31 * m ) 12 )) 7 )

I do not know what is wrong on/in that line. I have been working with other languages but Haskell is kind of specific.

Upvotes: 0

Views: 111

Answers (2)

Display Name
Display Name

Reputation: 8128

Not sure what you wanted, but it's just syntax error. Maybe you wanted to check for equality (then use == instead of =). But it may not be the case, because then return type should be Bool, not String. Also, the example looks strange, because not all arguments of function weekday are used in its definition.

Update

I suspect, your code should be like this:

weekday :: Int -> Int -> Int -> String
weekday jahr monat tag =
  let name = (mod ( tag + x + (div ( 31 * m )  12 )) 7 )
  in
    case name of
    0 -> "Sonntag"
    1 -> "Montag"
    2 -> "Dienstag" 
    3 -> "Mittwoch"
    4 -> "Donnerstag"
    5 -> "Freitag"
    6 -> "Samstag"
  where                        
    y = jahr - ( div ( 14 - monat )  12 )   
    x = y + ( div y 4 ) - ( div y 100 ) + ( div y 400 )
    m = monat + (12 * ( div ( 14 - monat ) 12 )) - 2 

It even does work (although I didn't check result): http://ideone.com/YAKXTU

You should learn a bit more about meanings of essential Haskell constructs let and where, and there is a good book "Learn You a Haskell for Great Good!" which I recommend. Good luck!

Upvotes: 1

kqr
kqr

Reputation: 15028

You can use where bindings to introduce names in Haskell.

weekday jahr monat tag = <function body>
  where name = (mod ( tag + x + (div ( 31 * m )  12 )) 7 )

That is, you need to define your variables in a where clause to the function. You can read more about where clauses for example in Learn You a Haskell.

About your second question: You can use tabs in Haskell code, it's just that in Haskell code, we rarely talk about "indented blocks of code" – the place where tabs are good. We mostly try to align function arguments, and spaces are way better for alignment.

Upvotes: 0

Related Questions