packages feed

disco-0.1.1.0: example/truthtable.disco

import string

||| Print a truth table for a binary boolean operation.  Intended to be used with
||| the :print command, as in
|||
|||   :print truthtable(~/\~)
!!! truthtable(~/\~) == "| F | F | F |\n| F | T | F |\n| T | F | F |\n| T | T | T |"
truthtable : (Bool * Bool -> Bool) -> List(Char)
truthtable(op) =
  unlines [ fmtrow [x,y,op(x,y)] | x <- enumerate(Bool), y <- enumerate(Bool) ]

||| Format a row of Boolean values with intervening | characters.
!!! fmtrow [true, false, false, true] == "| T | F | F | T |"
fmtrow : List(Bool) -> List(Char)
fmtrow(bs) = append("|", concat([ append(fmtbool(b), " |") | b <- bs ]))

fmtbool : Bool -> List(Char)
fmtbool(true) = " T"
fmtbool(false) = " F"