943,553 Members | Top Members by Rank

Ad:
Feb 2nd, 2009
0

Beginners Haskell Error Please Help

Expand Post »
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.
Similar Threads
Reputation Points: 39
Solved Threads: 1
Junior Poster in Training
chunalt787 is offline Offline
84 posts
since Apr 2008
Feb 2nd, 2009
2

Re: Beginners Haskell Error Please Help

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?
Last edited by Rashakil Fol; Feb 2nd, 2009 at 8:19 pm.
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Feb 3rd, 2009
0

Re: Beginners Haskell Error Please Help

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:
HASKELL Syntax (Toggle Plain Text)
  1. binarysum xs ys = binadd (reverse xs) (reverse ys) False
  2.  
  3. binadd (x:xs) (y:ys) n = if (n == False) then do
  4. if (x && y) then (binadd xs ys True ++ [True])
  5. else if (x || y) then (binadd xs ys False ++ [True]) {- Line 10 -}
  6. else (binadd xs ys False ++ [False])
  7. else do
  8. if (n && x && y) then (binadd xs ys True ++ [True])
  9. else if ((n && y) || (n && x)) then (binadd xs ys True ++ [False])
  10. else (binadd xs ys False ++ [True])
Error:
Exception C:\...: (10,0) - (17,72): Non-exhaustive patterns in function binadd
Reputation Points: 39
Solved Threads: 1
Junior Poster in Training
chunalt787 is offline Offline
84 posts
since Apr 2008
Feb 3rd, 2009
2

Re: Beginners Haskell Error Please Help

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.
Team Colleague
Reputation Points: 1133
Solved Threads: 171
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Feb 3rd, 2009
0

Re: Beginners Haskell Error Please Help

That worked. Thank you so much for all your help you have saved me and I really appreciate it.
Reputation Points: 39
Solved Threads: 1
Junior Poster in Training
chunalt787 is offline Offline
84 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Legacy and Other Languages Forum Timeline: INI file and UserProfile
Next Thread in Legacy and Other Languages Forum Timeline: Batch Insert Lines to an INI file problem : BLANK LINES?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC