99 questions/46 to 50
From HaskellWiki
(→Problem 48) 

(28 intermediate revisions by 11 users not shown)  
Line 1:  Line 1:  
__NOTOC__ 
__NOTOC__ 

−  These are Haskell translations of [http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L99_NinetyNine_Lisp_Problems.html Ninety Nine Lisp Problems]. 
+  This is part of [[H99:_NinetyNine_Haskell_ProblemsNinetyNine Haskell Problems]], based on [https://prof.ti.bfh.ch/hew1/informatik3/prolog/p99/ NinetyNine Prolog Problems]. 
−  
−  If you want to work on one of these, put your name in the block so we know someone's working on it. Then, change n in your block to the appropriate problem number, and fill in the <Problem description>,<example in lisp>,<example in Haskell>,<solution in haskell> and <description of implementation> fields. 

+  == Logic and Codes == 

−  == Problem 41 == 
+  == Problem 46 == 
−  <Problem description> 
+  (**) 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. 
−  <pre> 
+  A logical expression in two variables can then be written as in the following example: and(or(A,B),nand(A,B)). 
−  Example: 

−  <example in lisp> 

−  Example in Haskell: 
+  Now, write a predicate table/3 which prints the truth table of a given logical expression in two variables. 
−  <example in Haskell> 

−  </pre> 

−  Solution: 
+  Example: 
−  <haskell> 

−  <solution in haskell> 

−  </haskell> 

−  
−  <description of implementation> 

−  
−  == Problem 42 == 

−  
−  <Problem description> 

<pre> 
<pre> 

−  Example: 
+  (table A B (and A (or A B))) 
−  <example in lisp> 
+  true true true 
+  true fail true 

+  fail true fail 

+  fail fail fail 

+  </pre> 

Example in Haskell: 
Example in Haskell: 

−  <example in Haskell> 

−  </pre> 

−  Solution: 

<haskell> 
<haskell> 

−  <solution in haskell> 
+  > table (\a b > (and' a (or' a b))) 
+  True True True 

+  True False True 

+  False True False 

+  False False False 

</haskell> 
</haskell> 

−  <description of implementation> 
+  [[99 questions/Solutions/46  Solutions]] 
−  
−  == Problem 43 == 

−  <Problem description> 
+  == Problem 47 == 
−  <pre> 
+  (*) Truth tables for logical expressions (2). 
−  Example: 

−  <example in lisp> 

−  Example in Haskell: 
+  Continue problem P46 by defining and/2, or/2, etc as being operators. This allows to write the logical expression in the more natural way, as in the example: A and (A or not B). Define operator precedence as usual; i.e. as in Java. 
−  <example in Haskell> 

−  </pre> 

−  Solution: 
+  Example: 
−  <haskell> 

−  <solution in haskell> 

−  </haskell> 

−  
−  <description of implementation> 

−  
−  == Problem 44 == 

−  
−  <Problem description> 

<pre> 
<pre> 

−  Example: 
+  * (table A B (A and (A or not B))) 
−  <example in lisp> 
+  true true true 
+  true fail true 

+  fail true fail 

+  fail fail fail 

+  </pre> 

Example in Haskell: 
Example in Haskell: 

−  <example in Haskell> 

−  </pre> 

−  Solution: 

<haskell> 
<haskell> 

−  <solution in haskell> 
+  > table2 (\a b > a `and'` (a `or'` not b)) 
+  True True True 

+  True False True 

+  False True False 

+  False False False 

</haskell> 
</haskell> 

−  <description of implementation> 
+  [[99 questions/Solutions/47  Solutions]] 
−  == Problem 45 == 
+  == Problem 48 == 
−  <Problem description> 
+  (**) Truth tables for logical expressions (3). 
+  
+  Generalize problem P47 in such a way that the logical expression may contain any number of logical variables. Define table/2 in a way that table(List,Expr) prints the truth table for the expression Expr, which contains the logical variables enumerated in List. 

−  <pre> 

Example: 
Example: 

−  <example in lisp> 

−  Example in Haskell: 
+  <pre> 
−  <example in Haskell> 
+  * (table (A,B,C) (A and (B or C) equ A and B or A and C)) 
+  true true true true 

+  true true fail true 

+  true fail true true 

+  true fail fail true 

+  fail true true true 

+  fail true fail true 

+  fail fail true true 

+  fail fail fail true 

</pre> 
</pre> 

−  Solution: 
+  Example in Haskell: 
+  
<haskell> 
<haskell> 

−  <solution in haskell> 
+  > tablen 3 (\[a,b,c] > a `and'` (b `or'` c) `equ'` a `and'` b `or'` a `and'` c) 
+   infixl 3 `equ'` 

+  True True True True 

+  True True False True 

+  True False True True 

+  True False False True 

+  False True True True 

+  False True False True 

+  False False True True 

+  False False False True 

+  
+   infixl 7 `equ'` 

+  True True True True 

+  True True False True 

+  True False True True 

+  True False False False 

+  False True True False 

+  False True False False 

+  False False True False 

+  False False False False 

</haskell> 
</haskell> 

−  <description of implementation> 
+  [[99 questions/Solutions/48  Solutions]] 
−  
−  == Problem 46 == 

−  <Problem description> 
+  == Problem 49 == 
−  <pre> 
+  (**) Gray codes. 
−  Example: 

−  <example in lisp> 

−  Example in Haskell: 
+  An nbit Gray code is a sequence of nbit strings constructed according to certain rules. For example, 
−  <example in Haskell> 

−  </pre> 

−  
−  Solution: 

−  <haskell> 

−  <solution in haskell> 

−  </haskell> 

−  
−  <description of implementation> 

−  
−  == Problem 47 == 

−  
−  <Problem description> 

<pre> 
<pre> 

−  Example: 
+  n = 1: C(1) = ['0','1']. 
−  <example in lisp> 
+  n = 2: C(2) = ['00','01','11','10']. 
−  +  n = 3: C(3) = ['000','001','011','010',´110´,´111´,´101´,´100´]. 

−  Example in Haskell: 

−  <example in Haskell> 

</pre> 
</pre> 

−  Solution: 
+  Find out the construction rules and write a predicate with the following specification: 
−  <haskell> 

−  <solution in haskell> 

−  </haskell> 

−  
−  <description of implementation> 

−  
−  == Problem 48 == 

−  
−  <Problem description> 

<pre> 
<pre> 

−  Example: 
+  % gray(N,C) : C is the Nbit Gray code 
−  <example in lisp> 

−  
−  Example in Haskell: 

−  <example in Haskell> 

</pre> 
</pre> 

−  Solution: 
+  Can you apply the method of "result caching" in order to make the predicate more efficient, when it is to be used repeatedly? 
−  <haskell> 

−  <solution in haskell> 

−  </haskell> 

−  
−  <description of implementation> 

−  
−  == Problem 49 == 

−  
−  <Problem description> 

−  
−  <pre> 

−  Example: 

−  <example in lisp> 

Example in Haskell: 
Example in Haskell: 

−  <example in Haskell> 

−  </pre> 

−  Solution: 

<haskell> 
<haskell> 

−  <solution in haskell> 
+  P49> gray 3 
+  ["000","001","011","010","110","111","101","100"] 

</haskell> 
</haskell> 

−  <description of implementation> 
+  [[99 questions/Solutions/49  Solutions]] 
+  
== Problem 50 == 
== Problem 50 == 

−  <Problem description> 
+  (***) Huffman codes. 
+  
+  We suppose a set of symbols with their frequencies, given as a list of fr(S,F) terms. Example: [fr(a,45),fr(b,13),fr(c,12),fr(d,16),fr(e,9),fr(f,5)]. Our objective is to construct a list hc(S,C) terms, where C is the Huffman code word for the symbol S. In our example, the result could be Hs = [hc(a,'0'), hc(b,'101'), hc(c,'100'), hc(d,'111'), hc(e,'1101'), hc(f,'1100')] [hc(a,'01'),...etc.]. The task shall be performed by the predicate huffman/2 defined as follows: 

<pre> 
<pre> 

−  Example: 
+  % huffman(Fs,Hs) : Hs is the Huffman code table for the frequency table Fs 
−  <example in lisp> 
+  </pre> 
Example in Haskell: 
Example in Haskell: 

−  <example in Haskell> 

−  </pre> 

−  
−  Solution: 

<haskell> 
<haskell> 

−  <solution in haskell> 
+  *Exercises> huffman [('a',45),('b',13),('c',12),('d',16),('e',9),('f',5)] 
+  [('a',"0"),('b',"101"),('c',"100"),('d',"111"),('e',"1101"),('f',"1100")] 

</haskell> 
</haskell> 

−  <description of implementation> 
+  [[99 questions/Solutions/50  Solutions]] 
−  
[[Category:Tutorials]] 
[[Category:Tutorials]] 
Latest revision as of 13:30, 14 January 2012
This is part of NinetyNine Haskell Problems, based on NinetyNine Prolog Problems.
[edit] 1 Logic and Codes
[edit] 2 Problem 46
(**) 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 Haskell:
> table (\a b > (and' a (or' a b))) True True True True False True False True False False False False
[edit] 3 Problem 47
(*) Truth tables for logical expressions (2).
Continue problem P46 by defining and/2, or/2, etc as being operators. This allows to write the logical expression in the more natural way, as in the example: A and (A or not B). Define operator precedence as usual; i.e. as in Java.
Example:
* (table A B (A and (A or not B))) true true true true fail true fail true fail fail fail fail
Example in Haskell:
> table2 (\a b > a `and'` (a `or'` not b)) True True True True False True False True False False False False
[edit] 4 Problem 48
(**) Truth tables for logical expressions (3).
Generalize problem P47 in such a way that the logical expression may contain any number of logical variables. Define table/2 in a way that table(List,Expr) prints the truth table for the expression Expr, which contains the logical variables enumerated in List.
Example:
* (table (A,B,C) (A and (B or C) equ A and B or A and C)) true true true true true true fail true true fail true true true fail fail true fail true true true fail true fail true fail fail true true fail fail fail true
Example in Haskell:
> tablen 3 (\[a,b,c] > a `and'` (b `or'` c) `equ'` a `and'` b `or'` a `and'` c)  infixl 3 `equ'` True True True True True True False True True False True True True False False True False True True True False True False True False False True True False False False True  infixl 7 `equ'` True True True True True True False True True False True True True False False False False True True False False True False False False False True False False False False False
[edit] 5 Problem 49
(**) Gray codes.
An nbit Gray code is a sequence of nbit 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 Nbit 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 Haskell:
P49> gray 3 ["000","001","011","010","110","111","101","100"]
[edit] 6 Problem 50
(***) Huffman codes.
We suppose a set of symbols with their frequencies, given as a list of fr(S,F) terms. Example: [fr(a,45),fr(b,13),fr(c,12),fr(d,16),fr(e,9),fr(f,5)]. Our objective is to construct a list hc(S,C) terms, where C is the Huffman code word for the symbol S. In our example, the result could be Hs = [hc(a,'0'), hc(b,'101'), hc(c,'100'), hc(d,'111'), hc(e,'1101'), hc(f,'1100')] [hc(a,'01'),...etc.]. The task shall be performed by the predicate huffman/2 defined as follows:
% huffman(Fs,Hs) : Hs is the Huffman code table for the frequency table Fs
Example in Haskell:
*Exercises> huffman [('a',45),('b',13),('c',12),('d',16),('e',9),('f',5)] [('a',"0"),('b',"101"),('c',"100"),('d',"111"),('e',"1101"),('f',"1100")]