
-----------------------------------
mendes
Thu Dec 07, 2006 11:35 am

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  (x: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 (x: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

-----------------------------------
mendes
Thu Dec 07, 2006 11:40 am


-----------------------------------
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

-----------------------------------
mendes
Thu Dec 07, 2006 4:21 pm


-----------------------------------
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

-----------------------------------
mendes
Thu Dec 07, 2006 4:50 pm


-----------------------------------
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

binaryAdd3 zs ds fs = binaryAdd x(binaryAdd y z) 
binaryAdd

-----------------------------------
Hikaru79
Thu Dec 07, 2006 10:03 pm


-----------------------------------
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 :D

-----------------------------------
mendes
Fri Dec 08, 2006 2:30 am


-----------------------------------
am having problem in the 1 + 1 =  0 carry 1..
how am gonna add this carry to the next one..

thats my problem

-----------------------------------
wtd
Fri Dec 08, 2006 11:01 am


-----------------------------------
Is a number like 4 represented as:

[1, 0, 0]

or:

[0, 0, 1]

-----------------------------------
mendes
Fri Dec 08, 2006 11:23 am


-----------------------------------
it is represented as

[1,0,0]

-----------------------------------
Lazy
Mon Dec 25, 2006 8:03 pm


-----------------------------------
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.


getHeads :: [[a]] -> [a]
getHeads = (map head). (filter (not . null))

computeHD :: (Ord a) => [a]-> [a] -> Int
computeHD a b = length . filter (uncurry(/=)) $ zip a b

parity :: Int -> Int -> Int
parity 0 1 = 1
parity 1 0 = 1
parity 1 1 = 0
parity 0 0 = 0

computeParity :: [Int] -> [Int]
computeParity n = n ++ [foldl1 parity n]

carry :: Int -> Int -> Int
carry 1 1 = 1
carry _ _ = 0

adjustLength a b
	| a == b = (a,b)
	| a < b = (lengthen a b,b)
	| a > b = (a, lengthen b a)
	where lengthen x y = (take ((length y) - (length x)) (repeat 0)) ++ x

addBinary a b = addBinary' $ adjustLength a b where
	addBinary' :: ([Int], [Int]) -> [Int]
	addBinary' (a,b)
		| (foldl1 (+) b) == 0 = (dropWhile (==0)) a
		| otherwise = addBinary (parities a b) (carries a b) where
	 	parities x y = [0] ++ (map (uncurry parity) (zip a b))
		carries x y = (map (uncurry carry) (zip a b)) ++ [0] 


-----------------------------------
wtd
Mon Dec 25, 2006 8:34 pm


-----------------------------------
My inclination would be to avoid names like addBinary, and just create a Binary data type, which is an instance of Num.

-----------------------------------
Lazy
Mon Dec 25, 2006 9:10 pm


-----------------------------------
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.
