Beginners Haskell Error Please Help

Thread Solved

Join Date: Apr 2008
Posts: 61
Reputation: chunalt787 is an unknown quantity at this point 
Solved Threads: 1
chunalt787 chunalt787 is offline Offline
Junior Poster in Training

Beginners Haskell Error Please Help

 
0
  #1
Feb 2nd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Beginners Haskell Error Please Help

 
1
  #2
Feb 2nd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 61
Reputation: chunalt787 is an unknown quantity at this point 
Solved Threads: 1
chunalt787 chunalt787 is offline Offline
Junior Poster in Training

Re: Beginners Haskell Error Please Help

 
0
  #3
Feb 3rd, 2009
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Beginners Haskell Error Please Help

 
0
  #4
Feb 3rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 61
Reputation: chunalt787 is an unknown quantity at this point 
Solved Threads: 1
chunalt787 chunalt787 is offline Offline
Junior Poster in Training

Re: Beginners Haskell Error Please Help

 
0
  #5
Feb 3rd, 2009
That worked. Thank you so much for all your help you have saved me and I really appreciate it.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Legacy and Other Languages Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC