|
|
>>> I don't know why it works... but it does.
>>
>>> >module CDM where
>>> >
>>> >encode :: [Int] -> [Int] -> [Int]
>>> >encode cs xs = do
>>> > x <- xs
>>> > c <- cs
>>> > let y = if x == 1 then 1 else -1
>>> > return (c*y)
>>> >
>>> >decode :: [Int] -> [Int] -> [Int]
>>> >decode cs [] = []
>>> >decode cs xs =
>>> > let
>>> > (xs0,xs1) = splitAt (length cs) xs
>>> > y = sum (zipWith (*) cs xs0)
>>> > in ((y `min` 1) `max` 0) : decode cs xs1
>>
>>> OK, so let's encode something:
>>
>>> *CDM> encode [1,-1] [1, 0, 1]
>>> [1,-1,-1,1,1,-1]
>>
>>> *CMD> decode [1,-1] [1,-1,-1,1,1,-1]
>>> [1,0,1]
>>
>>> Well that's nice. But now check this out:
>>
>>> *CDM> let x1 = encode [1,1,1,1] [1,1,0,1]
>>> *CDM> let x2 = encode [1,-1,1,-1] [1,0,1,1]
>>> *CDM> let x3 = encode [1,1,-1,-1] [0,0,1,1]
>>> *CDM> let x4 = encode [1,-1,-1,1] [1,1,1,0]
>>> *CDM> x1
>>> [1,1,1,1,1,1,1,1,-1,-1,-1,-1,1,1,1,1]
>>> *CDM> x2
>>> [1,-1,1,-1,-1,1,-1,1,1,-1,1,-1,1,-1,1,-1]
>>> *CDM> x3
>>> [-1,-1,1,1,-1,-1,1,1,1,1,-1,-1,1,1,-1,-1]
>>> *CDM> x4
>>> [1,-1,-1,1,1,-1,-1,1,1,-1,-1,1,-1,1,1,-1]
>>> *CDM> let y = foldl1 (zipWith (+)) [x1,x2,x3,x4]
>>> *CDM> y
>>> [2,-2,2,2,0,0,0,4,2,-2,-2,-2,2,2,2,-2]
>>> *CDM> decode [1,1,1,1] y
>>> [1,1,0,1]
>>> *CDM> decode [1,-1,1,-1] y
>>> [1,0,1,1]
>>> *CDM> decode [1,1,-1,-1] y
>>> [0,0,1,1]
>>> *CDM> decode [1,-1,-1,1] y
>>> [1,1,1,0]
>>
>>> Magic!
>>
>> Are you seriously expecting people to understand what's going on there,
>> without any kind of explanation?
>>
>> I'm a programmer and I haven't the slightest idea.
>
> encode <key> <data> gives you a bunch of stuff. decode <key> <stuff> gives
> you back the original data.
>
> Which isn't that surprising. What's surprising is that if you add several
> of these together, the decode function still works. (Provided each data
> stream has a different key - or, more exactly, an "orthoganol" key.)
>
> Apparently this is how mobile phones manage to all transmit at the same
> time yet not screw up each other's transmissions.
>
> This much I would have thought would be vaguely intuitive from the
> program's output. Of course, to comprehend how I've implemented the
> encode/decode functions, you'd need to understand Haskell...
No wonder cell phones keep cutting out.
All it takes for a bit to be lost is for one analog "chip" to be
a little outside of it's intended magnitude and then it misses
the whole bit.
Post a reply to this message
|
|