Created
August 8, 2011 13:10
-
-
Save shawndumas/1131709 to your computer and use it in GitHub Desktop.
Starting with NAND only...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type LogicGate = | |
| ON | |
| OFF | |
| NAND of LogicGate * LogicGate | |
| NOT of LogicGate | |
| AND of LogicGate * LogicGate | |
| OR of LogicGate * LogicGate | |
| NOR of LogicGate * LogicGate | |
| XOR of LogicGate * LogicGate | |
| XNOR of LogicGate * LogicGate | |
let rec evaluate input = | |
match input with | |
| ON -> true | |
| OFF -> false | |
| NAND(a, b) -> not (evaluate a && evaluate b) | |
| NOT(a) -> evaluate (NAND(a, a)) | |
| AND(a, b) -> evaluate (NOT(NAND(a, b))) | |
| OR(a, b) -> evaluate (NAND(NOT(a), NOT(b))) | |
| NOR(a, b) -> evaluate (NOT(OR(a, b))) | |
| XOR(a, b) -> evaluate (AND(NAND(a, b), OR(a, b))) | |
| XNOR(a, b) -> evaluate (NOT(XOR(a, b))) | |
[ | |
NAND(OFF, OFF); | |
NAND(OFF, ON); | |
NAND(ON, OFF); | |
NAND(ON, ON) | |
] |> List.map (fun x -> printfn (evaluate x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment