3 people like it.

Truth Table Generator

This program is a mess, really - just ignore support functions. It'll be great if you can help me tidy up. Here's the passcode: "Passcode". The version of code below may not be original.

  1: 
  2: 
  3: 
  4: 
  5: 
  6: 
  7: 
  8: 
  9: 
 10: 
 11: 
 12: 
 13: 
 14: 
 15: 
 16: 
 17: 
 18: 
 19: 
 20: 
 21: 
 22: 
 23: 
 24: 
 25: 
 26: 
 27: 
 28: 
 29: 
 30: 
 31: 
 32: 
 33: 
 34: 
 35: 
 36: 
 37: 
 38: 
 39: 
 40: 
 41: 
 42: 
 43: 
 44: 
 45: 
 46: 
 47: 
 48: 
 49: 
 50: 
 51: 
 52: 
 53: 
 54: 
 55: 
 56: 
 57: 
 58: 
 59: 
 60: 
 61: 
 62: 
 63: 
 64: 
 65: 
 66: 
 67: 
 68: 
 69: 
 70: 
 71: 
 72: 
 73: 
 74: 
 75: 
 76: 
 77: 
 78: 
 79: 
 80: 
 81: 
 82: 
 83: 
 84: 
 85: 
 86: 
 87: 
 88: 
 89: 
 90: 
 91: 
 92: 
 93: 
 94: 
 95: 
 96: 
 97: 
 98: 
 99: 
100: 
101: 
102: 
103: 
104: 
105: 
106: 
107: 
108: 
109: 
110: 
111: 
112: 
113: 
114: 
115: 
116: 
117: 
118: 
119: 
120: 
121: 
122: 
123: 
124: 
125: 
126: 
127: 
128: 
129: 
130: 
131: 
132: 
133: 
134: 
135: 
136: 
137: 
138: 
139: 
140: 
141: 
142: 
143: 
144: 
145: 
146: 
147: 
148: 
149: 
150: 
151: 
152: 
153: 
154: 
155: 
156: 
157: 
158: 
159: 
160: 
161: 
162: 
163: 
164: 
165: 
166: 
167: 
open System
open System.Data
open System.Linq

/// Push a [string] to the [int]-th element of a [string].
let StrPush (sample : string) (cell : int) (input : string) : string = 
    //  What an elegant pattern matching !
    match cell with 
        | cell when cell <= 0 
            -> input.Insert( 0 , sample )
        | cell when cell >= input.Length 
            -> input.Insert( input.Length , sample )
        | _ -> input.Insert( cell , sample )   
    
/// Toss a [string] out of a [string]. 
let Toss (wipe : string) (input : string) : string =
    
    let mutable result = input

    while (result.Contains wipe) do
        result <- result.Remove ((result.IndexOf wipe) , (wipe.Length))

    result

/// The [string] morphs into a [string] in a [string].
let Morph (replace : string) (by : string) (input : string) : string =
    
    let mutable result = input

    while (result.Contains replace) do
        let checkpoint : int = result.IndexOf replace
        result <- result.Remove( checkpoint , (replace.Length) )
        result <- StrPush by checkpoint result

    result

/// Remove all ' ' characters in two ends of a string.
let _NO_ (input : string) : string = (input.TrimStart [| ' ' |]).TrimEnd [| ' ' |]

/// This function takes every word in a string then puts them in a string[].
let StrArr (input: string) : string[] = 

    (Array.filter (fun s -> s <> " " ) (input.Split(' ')))
        .Distinct().ToArray()

/// Counts words in a string.
let Count (input: string) : int = 

    (StrArr input).Length

/// Join all elements in a [string[]] with a [string] between them.
let StrJoin (input : string[]) (xtra : string) : string = 

    String.Join ( xtra , input )

/// Any ['T] type element in a ['T] list ?
let Any (id : 'T) (clist : 'T list) : bool =
    
    List.exists ( fun elem -> elem = id ) clist

let Sig (input : string) : string[] = 

    let result = 
        input 
        |> Toss "(" 
        |> Toss ")"
        |> Toss "and" 
        |> Toss "or"
        |> Toss "not" 
        |> Toss "True" 
        |> Toss "False"
        |> _NO_ 
        |> Morph "  " " "

    StrArr result

let BOOL (input : string) :  string =
       
    sprintf " %s "
        (input.ToUpper()
        |> Morph "("        "PARL"
        |> Morph ")"        "PARR"
        |> Morph "PARL"     " ( "
        |> Morph "PARR"     " ) "
        |> Morph "1"        " True "
        |> Morph "TRUE"     " True "
        |> Morph "0"        " False "
        |> Morph "FALSE"    " False "
        |> Morph "OR"       "  or "
        |> Morph "+"        "  or "
        |> Morph "|"        "  or "
        |> Morph "||"       "  or "
        |> Morph "AND"      " and "
        |> Morph "."        " and "
        |> Morph "*"        " and "
        |> Morph "&"        " and "
        |> Morph "&&"       " and "
        |> Morph "NOT"      " not "
        |> Morph "!"        " not "
        |> Morph "~"        " not "
        |> Morph "  "       " ")

let Core (xpression : string) : string =

    let data : DataTable = new DataTable()

    match string (data.Compute(xpression, null)) with
        | "True"    -> "1"
        |   _       -> "0"

let TruthTable =
    
    printf "\n  >>  WELCOME TO THE TRUTH TABLE PROGRAM 1.0 !\n"
    printf "\n  >>  Please enter expression for output: "

    let mutable xpression = Console.ReadLine()
    printf "\n\t >>  %s\n" (xpression)
    xpression <- xpression |> BOOL
    printf "\n\t ->  %s" (xpression |> _NO_)

    let var : string[] = Sig xpression

    let mutable var2 = [| for i in 0 .. var.Length - 1 -> "" |]

    let mutable temp = xpression
    for i in 0 .. var.Length - 1 do
       
        temp <- 
            sprintf " %s " temp
            |> Morph (sprintf " %s " var.[i]) " @@ "
            |> Morph " @@ " (sprintf " [%s] " var.[i])
    
    printfn "\n"
    printf "\n\t%s\n\n" ( String.Join ( "  " , [| for i in 0 .. 12 -> "-" |]) )

    for i : int in 0 .. 1 .. int ( 2. ** float (var.Length) ) - 1 do
        
        let mutable temp2 = temp
        if i = 0 then 
            for j : int in 0 .. 1 .. var.Length - 1 do printf "\t%s  " var.[j]
            printf "\t  %s\n\n" ("OUTPUT")

        let mutable foo : string = Convert.ToString ( i , 2 ) 
        if foo.Length < var.Length 
        then 
            foo <- String.Concat( String.replicate (var.Length - foo.Length) "0") + foo  

        for k : int in 0 .. var.Length - 1 do 
            
            var2.[k] <- foo.[k].ToString()
            printf "\t%s  " var2.[k]

        for l : int in 0 .. var.Length - 1 do
                
            temp2 <- Morph (sprintf "[%s]" var.[l] ) var2.[l] temp2

        printf "\t  %s\n" (temp2 |> BOOL |> Core) 

    printf "\n\t%s\n\n" ( String.Join ( "  " , [| for i in 0 .. 12 -> "-" |]) )

TruthTable

Console.ReadKey() |> ignore

//  Comments:

//  20200406 - My Name: Tell me something ...
namespace System
namespace System.Data
namespace System.Linq
val StrPush : sample:string -> cell:int -> input:string -> string

Full name: Script.StrPush


 Push a [string] to the [int]-th element of a [string].
val sample : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
val cell : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val input : string
String.Insert(startIndex: int, value: string) : string
property String.Length: int
val Toss : wipe:string -> input:string -> string

Full name: Script.Toss


 Toss a [string] out of a [string].
val wipe : string
val mutable result : string
(extension) Collections.Generic.IEnumerable.Contains<'TSource>(value: 'TSource) : bool
String.Contains(value: string) : bool
(extension) Collections.Generic.IEnumerable.Contains<'TSource>(value: 'TSource, comparer: Collections.Generic.IEqualityComparer<'TSource>) : bool
String.Remove(startIndex: int) : string
String.Remove(startIndex: int, count: int) : string
String.IndexOf(value: string) : int
String.IndexOf(value: char) : int
String.IndexOf(value: string, comparisonType: StringComparison) : int
String.IndexOf(value: string, startIndex: int) : int
String.IndexOf(value: char, startIndex: int) : int
String.IndexOf(value: string, startIndex: int, comparisonType: StringComparison) : int
String.IndexOf(value: string, startIndex: int, count: int) : int
String.IndexOf(value: char, startIndex: int, count: int) : int
String.IndexOf(value: string, startIndex: int, count: int, comparisonType: StringComparison) : int
val Morph : replace:string -> by:string -> input:string -> string

Full name: Script.Morph


 The [string] morphs into a [string] in a [string].
val replace : string
val by : string
val checkpoint : int
String.TrimStart([<ParamArray>] trimChars: char []) : string
val StrArr : input:string -> string []

Full name: Script.StrArr


 This function takes every word in a string then puts them in a string[].
type Array =
  member Clone : unit -> obj
  member CopyTo : array:Array * index:int -> unit + 1 overload
  member GetEnumerator : unit -> IEnumerator
  member GetLength : dimension:int -> int
  member GetLongLength : dimension:int -> int64
  member GetLowerBound : dimension:int -> int
  member GetUpperBound : dimension:int -> int
  member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
  member Initialize : unit -> unit
  member IsFixedSize : bool
  ...

Full name: System.Array
val filter : predicate:('T -> bool) -> array:'T [] -> 'T []

Full name: Microsoft.FSharp.Collections.Array.filter
val s : string
String.Split([<ParamArray>] separator: char []) : string []
String.Split(separator: string [], options: StringSplitOptions) : string []
String.Split(separator: char [], options: StringSplitOptions) : string []
String.Split(separator: char [], count: int) : string []
String.Split(separator: string [], count: int, options: StringSplitOptions) : string []
String.Split(separator: char [], count: int, options: StringSplitOptions) : string []
val Count : input:string -> int

Full name: Script.Count


 Counts words in a string.
val StrJoin : input:string [] -> xtra:string -> string

Full name: Script.StrJoin


 Join all elements in a [string[]] with a [string] between them.
val input : string []
val xtra : string
Multiple items
type String =
  new : value:char -> string + 7 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 2 overloads
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  member GetHashCode : unit -> int
  ...

Full name: System.String

--------------------
String(value: nativeptr<char>) : unit
String(value: nativeptr<sbyte>) : unit
String(value: char []) : unit
String(c: char, count: int) : unit
String(value: nativeptr<char>, startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
String(value: char [], startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : unit
String.Join(separator: string, values: Collections.Generic.IEnumerable<string>) : string
String.Join<'T>(separator: string, values: Collections.Generic.IEnumerable<'T>) : string
String.Join(separator: string, [<ParamArray>] values: obj []) : string
String.Join(separator: string, [<ParamArray>] value: string []) : string
String.Join(separator: string, value: string [], startIndex: int, count: int) : string
val Any : id:'T -> clist:'T list -> bool (requires equality)

Full name: Script.Any


 Any ['T] type element in a ['T] list ?
val id : 'T (requires equality)
val clist : 'T list (requires equality)
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
type bool = Boolean

Full name: Microsoft.FSharp.Core.bool
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member GetSlice : startIndex:int option * endIndex:int option -> 'T list
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val exists : predicate:('T -> bool) -> list:'T list -> bool

Full name: Microsoft.FSharp.Collections.List.exists
val elem : 'T (requires equality)
val Sig : input:string -> string []

Full name: Script.Sig
val result : string
val _NO_ : input:string -> string

Full name: Script._NO_


 Remove all ' ' characters in two ends of a string.
val BOOL : input:string -> string

Full name: Script.BOOL
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
String.ToUpper() : string
String.ToUpper(culture: Globalization.CultureInfo) : string
Multiple items
val Core : xpression:string -> string

Full name: Script.Core

--------------------
namespace Microsoft.FSharp.Core
val xpression : string
val data : DataTable
Multiple items
type DataTable =
  inherit MarshalByValueComponent
  new : unit -> DataTable + 2 overloads
  member AcceptChanges : unit -> unit
  member BeginInit : unit -> unit
  member BeginLoadData : unit -> unit
  member CaseSensitive : bool with get, set
  member ChildRelations : DataRelationCollection
  member Clear : unit -> unit
  member Clone : unit -> DataTable
  member Columns : DataColumnCollection
  member Compute : expression:string * filter:string -> obj
  ...

Full name: System.Data.DataTable

--------------------
DataTable() : unit
DataTable(tableName: string) : unit
DataTable(tableName: string, tableNamespace: string) : unit
DataTable.Compute(expression: string, filter: string) : obj
val TruthTable : unit

Full name: Script.TruthTable
val printf : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
val mutable xpression : string
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
Console.ReadLine() : string
val var : string []
val mutable var2 : string []
val i : int
property Array.Length: int
val mutable temp : string
val i : int32
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val mutable temp2 : string
val j : int
val mutable foo : string
type Convert =
  static val DBNull : obj
  static member ChangeType : value:obj * typeCode:TypeCode -> obj + 3 overloads
  static member FromBase64CharArray : inArray:char[] * offset:int * length:int -> byte[]
  static member FromBase64String : s:string -> byte[]
  static member GetTypeCode : value:obj -> TypeCode
  static member IsDBNull : value:obj -> bool
  static member ToBase64CharArray : inArray:byte[] * offsetIn:int * length:int * outArray:char[] * offsetOut:int -> int + 1 overload
  static member ToBase64String : inArray:byte[] -> string + 3 overloads
  static member ToBoolean : value:obj -> bool + 17 overloads
  static member ToByte : value:obj -> byte + 18 overloads
  ...

Full name: System.Convert
Convert.ToString(value: string) : string
   (+0 other overloads)
Convert.ToString(value: DateTime) : string
   (+0 other overloads)
Convert.ToString(value: decimal) : string
   (+0 other overloads)
Convert.ToString(value: float) : string
   (+0 other overloads)
Convert.ToString(value: float32) : string
   (+0 other overloads)
Convert.ToString(value: uint64) : string
   (+0 other overloads)
Convert.ToString(value: int64) : string
   (+0 other overloads)
Convert.ToString(value: uint32) : string
   (+0 other overloads)
Convert.ToString(value: int) : string
   (+0 other overloads)
Convert.ToString(value: uint16) : string
   (+0 other overloads)
String.Concat([<ParamArray>] values: string []) : string
   (+0 other overloads)
String.Concat(values: Collections.Generic.IEnumerable<string>) : string
   (+0 other overloads)
String.Concat<'T>(values: Collections.Generic.IEnumerable<'T>) : string
   (+0 other overloads)
String.Concat([<ParamArray>] args: obj []) : string
   (+0 other overloads)
String.Concat(arg0: obj) : string
   (+0 other overloads)
String.Concat(str0: string, str1: string) : string
   (+0 other overloads)
String.Concat(arg0: obj, arg1: obj) : string
   (+0 other overloads)
String.Concat(str0: string, str1: string, str2: string) : string
   (+0 other overloads)
String.Concat(arg0: obj, arg1: obj, arg2: obj) : string
   (+0 other overloads)
String.Concat(str0: string, str1: string, str2: string, str3: string) : string
   (+0 other overloads)
val replicate : count:int -> str:string -> string

Full name: Microsoft.FSharp.Core.String.replicate
val k : int32
val l : int32
Console.ReadKey() : ConsoleKeyInfo
Console.ReadKey(intercept: bool) : ConsoleKeyInfo
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
Raw view Test code New version

More information

Link:http://fssnip.net/7XE
Posted:4 years ago
Author:you
Tags: string , datatable