XOR Character Arrays in Haskell
Overview
Just what it sounds like, take 2 strings "abcd" and "efgh" and XOR them together to get a new string. There are custom libraries available, but nothing that's default in Hugs or GHC that I know of. It's a simple one liner, but not particularly intuitive ( Like anything in Haskell is :P )
Code
Interpreter session::module +Data.ByteString.Internal :module +Data.Bits let xorStrs x y = map w2c (zipWith xor (map c2w x) (map c2w y)) let c1 = "Test String" let c2 = "Hello World" xorStrs c1 c2
Non Prelude Functions
- c2w -- Character2Word changes an individual Char to a Word8 type
- w2c -- Reverse of c2w, converts a Word8 to a Char
- xor -- Only works on Word# types, does a bitwise or on 2 values
Comments(1)
2009-07-22 04:47:18
(2009-07-21 22:54:49) Chris Wellons said:
There are people that would argue that Haskell is more intuitive than any otherlanguage, and that your mind is only tainted by years of becoming accustomed to
unnatural imperative programming. There is a mention on the Haskell website
(http://www.haskell.org/haskellwiki/Why_Haskell_matters) and in this Free
Software Magazine article
(http://www.freesoftwaremagazine.com/articles/haskell).
"Haskell lets you write code in a surprisingly intuitive way. Reading Haskell
code is easy, and reasoning about Haskell code is easy, too. You’ll have less
need for a debugger with Haskell."
(What, no 'a' tags allowed? :-P)