Link to Snippet

Ninety-Nine F# Problems - Problems 46 - 50 - Logic and Codes

These are F# solutions of Ninety-Nine Haskell Problems which are themselves translations of Ninety-Nine Lisp Problems and Ninety-Nine Prolog Problems. The solutions are hidden so you can try to solve them yourself.

/// These are F# solutions of Ninety-Nine Haskell Problems /// (http://www.haskell.org/haskellwiki/H-99:_Ninety-Nine_Haskell_Problems), /// which are themselves translations of Ninety-Nine Lisp Problems/// (http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html)/// and Ninety-Nine Prolog Problems/// (https://sites.google.com/site/prologsite/prolog-problems).////// If you would like to contribute a solution or fix any bugs, send /// an email to paks at kitiara dot org with the subject "99 F# problems". /// I'll try to update the problem as soon as possible.////// The problems have different levels of difficulty. Those marked with a single asterisk (*) /// are easy. If you have successfully solved the preceeding problems you should be able to /// solve them within a few (say 15) minutes. Problems marked with two asterisks (**) are of /// intermediate difficulty. If you are a skilled F# programmer it shouldn't take you more than /// 30-90 minutes to solve them. Problems marked with three asterisks (***) are more difficult. /// You may need more time (i.e. a few hours or more) to find a good solution////// Though the problems number from 1 to 99, there are some gaps and some additions marked with /// letters. There are actually only 88 problems.

/// Define predicates and/2, or/2, nand/2, nor/2, xor/2, impl/2 and equ/2 (for logical /// equivalence) which succeed or fail according to the result of their respective /// operations; e.g. and(A,B) will succeed, if and only if both A and B succeed./// /// A logical expression in two variables can then be written as in the following example: /// and(or(A,B),nand(A,B))./// /// Now, write a predicate table/3 which prints the truth table of a given logical /// expression in two variables./// /// Example: /// (table A B (and A (or A B)))/// true true true/// true fail true/// fail true fail/// fail fail fail/// /// Example in F#: /// /// > table (fun a b -> (and' a (or' a b)));;/// true true true/// true false true/// false true false/// false false false/// val it : unit = ()(Solution)

/// An n-bit Gray code is a sequence of n-bit strings constructed according to certain rules./// For example,////// n = 1: C(1) = ['0','1']./// n = 2: C(2) = ['00','01','11','10']./// n = 3: C(3) = ['000','001','011','010',´110´,´111´,´101´,´100´]./// /// Find out the construction rules and write a predicate with the following specification:/// % gray(N,C) :- C is the N-bit Gray code/// /// Can you apply the method of "result caching" in order to make the predicate more efficient, /// when it is to be used repeatedly?/// /// Example in F#: /// /// P49> gray 3/// ["000","001","011","010","110","111","101","100"](Solution)

// The algorithm is explained here: http://en.wikipedia.org/wiki/Huffman_coding// The implementation below uses lists. For better performance use a priority queue.// This is how it works. First we transform the list of terms and frecuencies into a list of Leafs (6).// Then, before anything happpens, we sort the list to place the terms with the lowest frecuency// at the head of the List (1) (this is where a priority queue would shine). // Otherwise, we combine the first two elements into a Node with the combined frecuency of the two nodes (4). // We add the node to the list and try again (5). Eventualy the list is reduced to // one term and we're done constructing the tree (2). Once we have the tree, we just need to encode it (7).let huffman symbols = let rec createTree tree = let xs = tree |> List.sortBy frecuency (* 1 *) match xs with | [] -> failwith "Empty list" | [x] -> x (* 2 *) | x::y::xs -> (* 3 *) let ht = Node(frecuency x + frecuency y, x , y) (* 4 *) createTree (ht::xs) (* 5 *) let ht = symbols |> List.map(fun (a,f) -> Leaf (f,a)) (* 6 *) |> createTree encode ht |> List.sortBy(fst) (* 7 *)