Haskell: lexical error in string / character literal in 'i' character

I'm new to Haskell programming, and it's hard for me to understand why I am getting this error in my code.

My problem is this: any positive integer i can be expressed as i = 2 ^ n * k, where k is odd, i.e. as a power of 2 times an odd number. Call n the exponent 2 in i. For example, an indicator of 2 in 40 is 3 (because 40 = 2 ^ 3 * 5), while an indicator of 2 in 42 is 1. If I am odd, then n is zero. If, on the other hand, I am even, this means that it can be divided by 2. Write the exponentOfTwo function to determine the exponent 2 in the argument.

I understand psuedocode and it seems pretty simple: I recursively divide by 2 until the result is odd, the number of times the division happens - n

here is my code (line 31-32):

exponentOfTwo :: Int -> Int  
exponentOfTwo i = if odd i then 0 else 1 + exponentOfTwo (i 'div' 2)  

I get the error "lexical error in the literature of the line / character in the" i "character in row 32 of column 62.

I tried to look for a solution to this error everywhere, and so far I have been out of luck.

+5
source share
2 answers

To use a function in infix for, surround it with reverse windows (`), not single quotes ('). The latter relate to character literals, which, as a rule, have only one character.

+6
source

Are the characters around divbackquotes, not regular quotes? They must be in order for the function name to be used as an infix operator. I changed this in your definition and the code worked for me.

+2
source

All Articles