04k
04k

Reputation: 53

Parse error in pattern n

I'm new to haskell. Got a parse error and dont know what is wrong.

fromDecTo :: (Int -> Int) -> [Int]
fromDecTo (n x) | (n < x)   = [n]
                | otherwise = fromDecTo (n `div` x) ++ [n `mod` x]

This should convert a decimal number into a number with base x (a.e. hexadecimal).

Please help me.

Upvotes: 0

Views: 217

Answers (2)

chi
chi

Reputation: 116139

fromDecTo :: (Int -> Int) -> [Int]

This is a function having one argument, a function from integers to integers. This is not what you want: you want a function having two arguments, i.e.

fromDecTo :: Int -> Int -> [Int]

Similarly, the pattern

fromDecTo (n x)

expresses the result of applying a function n to x and then applying fromDecToto the result. This is not what you want, taking two arguments is done with

fromDecTo n x

As a final note, even if it not customary to do so, it is possible to add parentheses, but with the opposite association. The same type shown above could be written

fromDecTo :: Int -> (Int -> Int)

which stresses the fact that functions are curried in Haskell: a binary function is actually a function taking only one argument, the first, and returning a function taking one other argument, the second, and finally returning the result.

Similarly, instead of

fromDecTo n x

we could write

(fromDecTo n) x

since n is applied to fromDecTo first, and then x after that.

Upvotes: 4

Nyavro
Nyavro

Reputation: 8866

You have several issues in your code. Here is what you want to achieve:

fromDecTo :: Int -> Int -> [Int]
fromDecTo n x | (n < x)   = [n]
              | otherwise = fromDecTo (n `div` x) x ++ [n `mod` x]

Upvotes: 0

Related Questions