Computer Science Canada Help out in a homework - just new in haskell |
Author: | mendes [ Thu Dec 07, 2006 11:35 am ] |
Post subject: | Help out in a homework - just new in haskell |
questions Given a list of lists [L1; : : : ; Lk], the function getHeads returns the list of all first elements of all nonempty lists Li. For example, getHeads [[],[1],[2,3]] should return the list [1,2]. what i done.. getheads [] = [] getHeads (l:ls) = (head l):(getHeads ls) question Write a HASKELL function ComputeHD that takes 2 lists (of integers 0s and 1s) as an input and returns the hamming distance. The Hamming Distance is the minimum number of bit positions in which 2 codewords (lists) differ. (10 points) For example: ComputeHD [0,0,0,0,0,0,0,0,0] [0,0,0,0,0,1,1,1,1,1] is equal to 5. ComputeHD [0, 0, 1, 0] [0, 1, 1, 1] is equal to 2. Note that the lists (or codewords) need to be of equal length. what i done so far computeHd (b:rs) [] = 0 computeHd [] (c:us) = 0 computeHD (xs) (y:ys) |x == y = computeHD xs ys |otherwise = 1 + computeHD xs ys 10. Write a HASKELL function ComputeParity that takes one list (of integers 0s and 1s) as an input and returns the list with the parity (even or odd number of 1s) appended to it (0 even parity, 1 odd parity). For example: ComputeParity [0,1,1,1,1,0,1] returns [0,1,1,1,1,0,1,1] ComputeParity [0,1,1,1,1] returns [0,1,1,1,1,0] Note that the resulting list will always have an even number of ones (i.e. even parity). what I done so far computeparity [] = 0 computeparity (xs) | x == 1 = 1 + computeparity xs | otherwise = computeparity xs Please help me out in this question...there are other questions I was able to them..but this ones I am having trouble with.. thank you |
Author: | mendes [ Thu Dec 07, 2006 11:40 am ] |
Post subject: | |
And one more question Write a HASKELL function BinaryAdd3 that adds 3 binary numbers together. It takes 3 lists (of 0s and 1s) and adds them together and outputs a list. what i done add3 x y z = add x(add y z) how to do it binary?? Thx for any help |
Author: | mendes [ Thu Dec 07, 2006 4:21 pm ] |
Post subject: | |
Hey guys I dont need help for the above questions.I was able to them. However I am not able to do the binary thing. Can someone please give me hint or solution of how to write a binaryadd method to calculate binary numbers |
Author: | mendes [ Thu Dec 07, 2006 4:50 pm ] |
Post subject: | |
Hey guys , I am working on it...thats what i done the question is Write a HASKELL function BinaryAdd3 that adds 3 binary numbers together. It takes 3 lists (of 0s and 1s) and adds them together and outputs a list. I just need help in this.. thx Quote: binaryAdd3 zs ds fs = binaryAdd x(binaryAdd y z)
binaryAdd[] ts = [] binaryAdd rs [] = [] binaryAdd (x,xs) (y,ys) = | x==1 && y == 1 = [0] ++ (1+binaryAdd xs ys) | x == 1 && y == 0 = [1] ++ binaryAdd xs ys | x == 0 && y == 1 = [1] ++ binaryAdd xs ys | x == 0 && y == 0 = [0] ++ binaryAdd xs ys |
Author: | Hikaru79 [ Thu Dec 07, 2006 10:03 pm ] |
Post subject: | |
We're really not in the habit of just doing others' homework on demand, though perhaps if you asked some more specific questions about what you're finding difficult (as opposed to just "here's what I have so far, do the rest"), you'd find more ready help. On another note, what school/course are you taking that uses Haskell? I've heard of Miranda being used, but never Haskell (yet). That's neat |
Author: | mendes [ Fri Dec 08, 2006 2:30 am ] |
Post subject: | |
am having problem in the 1 + 1 = 0 carry 1.. how am gonna add this carry to the next one.. thats my problem |
Author: | wtd [ Fri Dec 08, 2006 11:01 am ] | ||||
Post subject: | |||||
Is a number like 4 represented as:
or:
|
Author: | mendes [ Fri Dec 08, 2006 11:23 am ] | ||
Post subject: | |||
it is represented as
|
Author: | Lazy [ Mon Dec 25, 2006 8:03 pm ] | ||
Post subject: | |||
Hi all, First of all, I wish you all guys a merry Christmas! I'm back home and finally have time to continue my Haskell studies. Here's how I'd solve the problems. getHeads is easy; remove empty lists from your list, and map head over the rest. computeHD filters out unequal pairs and counts how many there are. I could've probably done it with mapAccumL; suggestions are welcome. I'm quite proud of computeParity, which is a rather elegant fold - at least I think so. addBinary uses the following principle: 1. add leading zeros to the shorter list so both lists are same length. 2. zip them together, finding each tuple's parity and each tuple's carry value, the latter with an extra trailing zero (so it's shifted left). 3. If nothing is carried, the list of parities is the answer. Else, add the two lists and repeat recursively until the result is found. Once again, I must stress I'm a beginner, so this is likely not the most elegant nor effective solution. I enjoyed doing it however.
|
Author: | wtd [ Mon Dec 25, 2006 8:34 pm ] |
Post subject: | |
My inclination would be to avoid names like addBinary, and just create a Binary data type, which is an instance of Num. |
Author: | Lazy [ Mon Dec 25, 2006 9:10 pm ] |
Post subject: | |
Quote: My inclination would be to avoid names like addBinary, and just create a Binary data type, which is an instance of Num.
In a larger project, definitely. However, this is only an exercise in which most of the minimal definition of Num is never used, so I didn't bother defining it. |