Hacker News new | ask | show | jobs
by dxbydt 5427 days ago
a bit rusty, but think this one's right too ( am using Peirce's arrow instead of Sheffer stroke )

type LogicGate = | ON | OFF | NOR of LogicGate * LogicGate | NOT of LogicGate | AND of LogicGate * LogicGate | OR of LogicGate * LogicGate | NAND of LogicGate * LogicGate | XOR of LogicGate * LogicGate | XNOR of LogicGate * LogicGate

    let rec evaluate input =
        match input with
        | ON         -> true
        | OFF        -> false
        | NOR(a, b) -> not ((evaluate a) || (evaluate b))
        | NOT(a)     -> (evaluate (NOR(a, a)))
        | OR(a, b)  -> (evaluate (NOT(NOR(a, b))))
        | AND(a, b)   -> (evaluate (NOR(NOT(a), NOT(b))))
        | NAND(a, b)  -> (evaluate (NOT(AND(a, b))))
        | XNOR(a, b)  -> (evaluate (OR(AND(a, b), NOR(a, b))))
        | XOR(a, b) -> (evaluate (NOT(XNOR(a, b))))

    [
        NOR(OFF, OFF);
        NOR(OFF, ON);
        NOR(ON,  OFF);
        NOR(ON,  ON)
    ] |> List.map (fun x -> printfn (evaluate x))