Hamming Code (7,4) for Mathematica
Here's a simple (but not very elegant) Hamming code for Mathematica I made for an assignment.
The first function takes a List of 7 bits (like "{1,0,0,1,1,0,0}") and returns "{1,1,0,1,1,0,0}":
correct[word_List] := Module[{error, error2 = 0, error3 = 0}, error = 0; new = word; If[EvenQ[word[[1]] + word[[2]] + word[[4]] + word[[5]]], error = 0, error = 1]; If[EvenQ[word[[2]] + word[[3]] + word[[4]] + word[[7]]], error2 = 0, error2 = 1]; If[EvenQ[word[[1]] + word[[3]] + word[[4]] + word[[6]]], error3 = 0, error3 = 1]; If[error == 1 && error2 == 1 && error3 == 1, If[new[[4]] == 0, new[[4]] = 1, new[[4]] = 0]]; If[error == 1 && error2 == 1 && error3 == 0, If[new[[2]] == 0, new[[2]] = 1, new[[2]] = 0]]; If[error == 1 && error3 == 1 && error2 == 0, If[new[[1]] == 0, new[[1]] = 1, new[[1]] = 0]]; If[error2 == 1 && error3 == 1 && error == 0, If[new[[3]] == 0, new[[3]] = 1, new[[3]] = 0]]; If[error == 1 && error2 == 0 && error3 == 0, If[new[[5]] == 0, new[[5]] = 1, new[[5]] = 0]]; If[error == 0 && error3 == 1 && error2 == 0, If[new[[6]] == 0, new[[6]] = 1, new[[6]] = 0]]; If[error2 == 1 && error3 == 0 && error == 0, If[new[[7]] == 0, new[[7]] = 1, new[[7]] = 0]]; new]
As you can see it takes use of simple If[]s and a module. The second function takes a list of 4 bits ("{1,0,0}") and adds the missing 3 bits:
encode[{b1_, b2_, b3_, b4_}] := Module[{new}, new = {b1, b2, b3, b4, 0, 0, 0}; listNew = new; If[EvenQ[b1 + b2 + b4], listNew[[5]] = 0, listNew[[5]] = 1]; If[EvenQ[b1 + b3 + b4], listNew[[6]] = 0, listNew[[6]] = 1]; If[EvenQ[b2 + b3 + b4], listNew[[7]] = 0, listNew[[7]] = 1]; listNew]
And finally the last function simulates an error at a certain point ( the input will be "{1,1,0,1,1,0,0}, 2" and this returns "{1,0,0,1,1,0,0}":
errorAt[word_List, pos_] := (listN = word; If[listN[[pos]] == 0, listN[[pos]] = 1, listN[[pos]] = 0]; listN)

















