Skip to content

Instantly share code, notes, and snippets.

@shawndumas
Created August 8, 2011 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawndumas/1131709 to your computer and use it in GitHub Desktop.
Save shawndumas/1131709 to your computer and use it in GitHub Desktop.
Starting with NAND only...
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