By the way, here is the sample output:

Truth Table: OR

A B X

=====

F F F

F T T

T F T

T T T

Truth Table: AND

A B X

=====

F F F

F T F

T F F

T T T

By on 10/21/2007 7:13 PM ()

Here's an example using list comprehensions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 

#light

let truthTable f =
    { for a in [true; false] for b in [false; true] -> a, b, f a b }

let printTable header table =
    let boolToString = function true -> "T" | false -> "F"
    let printLine(a, b, x) = printf "%s %s %s\n" (boolToString a) (boolToString b) (boolToString x)
    printf "Truth Table: %s\nA B X\n=====\n" header
    table |> Seq.iter printLine
    
let testOr a b = a || b
let testAnd a b = a && b
printTable "OR" (truthTable testOr)
printTable "AND" (truthTable testAnd)
By on 10/22/2007 6:01 AM ()

Hey Ray,

Awesome, thanks. That really cleans things up a lot. :)

It seems that I keep finding ways to make it smaller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#light


let truthTable header f  =

  let b2s (b:bool) = b.ToString().ToUpper().Substring(0,1) in

    [ for a in [false; true]

      for b in [false; true] -> sprintf "%s %s %s\n" (b2s a) (b2s b) (b2s (f a b))

    ] |> List.fold_left (^) (sprintf "Truth Table: %s\nA B X\n=====\n" header)    


let testOr a b =  a || b

let testAnd a b = a && b

printfn "%s" (truthTable "OR" testOr)

printfn "%s" (truthTable "AND" testAnd)

Incidentally, what are the advantages of using lists, seqs, or arrays? You can change the brackets (which create a list) to curly braces (which create seq's), or even '[| .. |]' symbols to get arrays. Is one better than the other two?

Theorem: Any functionality currently implemented in N lines of F# code can easily be implemented in N-1 lines of code, for any value of N > 2. :P

-Matt

By on 10/22/2007 10:55 AM ()

I've definitely written a few proofs of your theorem myself! And it was nice to see your condensation above.

I'll give you my take on the Big Three collection types but I'm just a novice. Maybe someone with more experience will give a more complete explanation.

I mainly use Arrays when I need to interop with a non-F# library. For example, String has constructors that take a character array:

1
2
3
> open System;;
> new String([|'a';'b';'c'|]);;
val it : String = "abc"

A Sequence is another type that is used in interop'ing with other .NET libraries. Sequences implement the IEnumerable<T> interface which is ubiquitous in the framework:

1
2
3
4
5
6
7
8
9
10
11
12
 

> open System.Collections.Generic;;

> let nums = {1 .. 3};;

val nums : seq<int>

> let list = new List<int>(nums);;

val list : List<int>

If I'm not interop'ing with other .NET libraries, I'm pretty much just using Lists.

So, like everything, it depends on the context. The best thing to do is to get familiar with the each of the collection types and the functions that they support.

By on 10/22/2007 12:29 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper