I am just starting off in a haskell course and I am trying to write a program that takes a list representing an int such as [1,4,3] would mean 143, and convert it to binary and return it as a list in the same fashion. I have written the following code but I keep getting the same annoying error and I cant figure out how to fix it.

Code:

decimaltobinary :: [Int] -> [Int]
decimaltobinary xs = d2b2(d2b(reverse xs))

d2b :: [Int] -> Int
d2b [x] = x
d2b (x:xs) = x + d2b(map (10*) xs)

d2b2 :: Int -> [Int]
d2b2 0 = [0]
d2b2 1 = [1]
d2b2 n = if (n `mod` 2 == 1) then ( return ((d2b2(n / 2)):1))
                             else ( return ((d2b2(n / 2)):0))

Error:

Couldn't match expected type 'Int' against inferred type '[[Int]]' 
in the first argument of the return namely '((d2b2(n / 2)):1)' and so forth

I don't understand why its saying [[Int]] instead of just [Int] either.

d2b2 evaluates to something of type [Int] , right? Which means the expression (d2b2(n / 2):1) is trying to pass an [Int] as the left-hand argument of the (:) function. Since the (:) function is of type a -> [a] -> [a] , that means it expects on the right-hand side a list of whatever was on the left hand side. That means it expects a list of [Int] , i.e. something whose type is [[Int]] . Instead of an expression of type [[Int]] , you're supplying 1, or 0, something of type Int . That's the reason your error message is the way it is.

The function (:) is for appending elements to the left end of a list. If you want to append an element to the right end, wrap the element in a list an use a (++) operator. For example: [1,2,3] ++ [x] => [1,2,3,x] .

You have a number of other problems with your code. One of them has to do with this question: What do you expect 'return' to do?

commented: Great responses very informative and patient, thanks so much +1

Thank you very much that helped a ton and actually makes sense. I got one more for ya. Im trying to do a binary add now where the binary numbers are represented by a list of Bool's and for some reason its saying I have a non exhaustive list.

Code:

binarysum xs ys = binadd (reverse xs) (reverse ys) False

binadd (x:xs) (y:ys) n = if (n == False) then do 
                            if (x && y) then (binadd xs ys True ++ [True])
                                else if (x || y) then (binadd xs ys False ++ [True])  {- Line 10 -}
                                    else (binadd xs ys False ++ [False])
                         else do
                            if (n && x && y) then (binadd xs ys True ++ [True])
                                else if ((n && y) || (n && x)) then (binadd xs ys True ++ [False])
                                    else (binadd xs ys False ++ [True])

Error:

Exception C:\...: (10,0) - (17,72): Non-exhaustive patterns in function binadd

No, it says you have non-exhaustive patterns!

In particular, you defined the function binadd for non-empty lists with:

binadd (x:xs) (y:ys) n = ...

But you didn't define the function for empty lists! What is the value of binadd ([True]) [] True ? Your function's behavior is not defined, because the empty list [] doesn't match the pattern (y:ys) . You need new cases below to handle empty lists, probably something like

binadd xs [] n = ...
binadd [] ys n = ...

But whatever you do, make sure your function can handle all the cases. I'm pretty sure there is some warning you can turn on that will tell you about functions that don't handle all the conceivable cases.

That worked. Thank you so much for all your help you have saved me and I really appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.