Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help out in a homework - just new in haskell
Index -> Programming, General Programming -> Functional Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
mendes




PostPosted: 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 (xMads) (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 (xMads) | 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
Sponsor
Sponsor
Sponsor
sponsor
mendes




PostPosted: Thu Dec 07, 2006 11:40 am   Post subject: (No 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
mendes




PostPosted: Thu Dec 07, 2006 4:21 pm   Post subject: (No 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
mendes




PostPosted: Thu Dec 07, 2006 4:50 pm   Post subject: (No 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
Hikaru79




PostPosted: Thu Dec 07, 2006 10:03 pm   Post subject: (No 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 Very Happy
mendes




PostPosted: Fri Dec 08, 2006 2:30 am   Post subject: (No subject)

am having problem in the 1 + 1 = 0 carry 1..
how am gonna add this carry to the next one..

thats my problem
wtd




PostPosted: Fri Dec 08, 2006 11:01 am   Post subject: (No subject)

Is a number like 4 represented as:

code:
[1, 0, 0]


or:

code:
[0, 0, 1]
mendes




PostPosted: Fri Dec 08, 2006 11:23 am   Post subject: (No subject)

it is represented as

code:
[1,0,0]
Sponsor
Sponsor
Sponsor
sponsor
Lazy




PostPosted: Mon Dec 25, 2006 8:03 pm   Post subject: (No 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.

code:

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




PostPosted: Mon Dec 25, 2006 8:34 pm   Post subject: (No subject)

My inclination would be to avoid names like addBinary, and just create a Binary data type, which is an instance of Num.
Lazy




PostPosted: Mon Dec 25, 2006 9:10 pm   Post subject: (No 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.
Display posts from previous:   
   Index -> Programming, General Programming -> Functional Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: