i trying define function(s) helped me simulate basic operations when using 8-bit numbers.
i'm having hard time figuring 1 out.. i'm trying keep simple possible without importing anything, started 2 lists of 8 elements in (which 0's , 1's).
if i'm not mistaken should start looking this:
bitsum :: [int] -> [int] -> [int] bitsum [][] = []
after last line starts kind of tricky me because can't add 1 one elements lists.
bitsum (x:xs)(y:ys)
that's have right think correct.
my idea try this:
bitsum :: [int] -> [int] -> [int] bitsum [][] = [] bitsum (x:xs)[] = (x:xs) bitsum [](y:ys) = (y:ys) bitsum (x:xs)(y:ys) | (x:xs) == (y:ys) && < 0 = (x:xs) | (x:xs) == (y:ys) && > 0 =
but think i'm taking wrong turn somewhere.
i appreciate if give me hand problem.
you going need carry bit. can't add column column. let's piece piece:
bitsum :: [int] -> [int] -> [int] bitsum = bitsum' 0 bitsum' _ [] [] = []
there's start. start no carry, , deal case of no more bits add. if both bits 0?
bitsum' 0 (0:xs) (0:ys) = 0 : bitsum' 0 xs ys bitsum' 1 (0:xs) (0:ys) = 1 : bitsum' 0 xs ys
okay, if both 0, , carry 0, result bit 0 , no carry. if there carry use it, , continue without one.
bitsum' 0 (1:xs) (1:ys) = 0 : bitsum' 1 xs ys bitsum' 1 (1:xs) (1:ys) = 1 : bitsum' 1 xs ys
if one, similar. except there carry. , if different:
bitsum' 0 (x:xs) (y:ys) = 1 : bitsum' 0 xs ys bitsum' 1 (x:xs) (y:ys) = 0 : bitsum' 1 xs ys
well have 0 , 1 because we've dealt other cases, must add one. can figure out should that. start see patterns above, , can condensed end shorter answer.
bitsum :: [int] -> [int] -> [int] bitsum = bitsum' 0 bitsum' _ [] [] = [] bitsum' carry (x:xs) (y:ys) | x == y = carry : bitsum' x xs ys | otherwise = (1 - carry) : bitsum' carry xs ys
(1-carry) fancy way of flipping 1 0 or vice versa since in case bit opposite of carry.
Comments
Post a Comment