Personal tools

99 questions/46 to 50

From HaskellWiki

< 99 questions(Difference between revisions)
Jump to: navigation, search
m (minor syntactical/semantical fixes to solution 46)
m (solution for problem 48)
Line 26: Line 26:
   
 
Example in Haskell:
 
Example in Haskell:
> table (\a b -> (and' a (or' a b))
+
> table2 (\a b -> (and' a (or' a b))
 
True True True
 
True True True
 
True False True
 
True False True
Line 60: Line 60:
 
equ' _ _ = False
 
equ' _ _ = False
   
table f = putStrLn . unlines $ [show a ++ " " ++ show b ++ " " ++ show (f a b)
+
table2 :: (Bool -> Bool -> Bool) -> IO ()
| a <- [True, False], b <- [True, False]]
+
table2 f = putStrLn . unlines $ [show a ++ " " ++ show b ++ " " ++ show (f a b)
  +
| a <- [True, False], b <- [True, False]]
 
</haskell>
 
</haskell>
   
 
The implementations of the logic functions are quite verbose and can be shortened in places (like "equ' = (==)").
 
The implementations of the logic functions are quite verbose and can be shortened in places (like "equ' = (==)").
   
The table function in Lisp supposedly uses Lisp's symbol handling to substitute variables on the fly in the expression. I chose passing a binary function instead because parsing an expression would be more verbose in haskell than it is in Lisp.
+
The table function in Lisp supposedly uses Lisp's symbol handling to substitute variables on the fly in the expression. I chose passing a binary function instead because parsing an expression would be more verbose in haskell than it is in Lisp. Template Haskell could also be used :)
 
 
 
== Problem 47 ==
 
== Problem 47 ==
   
<Problem description>
+
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.
   
 
<pre>
 
<pre>
 
Example:
 
Example:
<example in lisp>
+
* (table A B (A and (A or not B)))
  +
true true true
  +
true fail true
  +
fail true fail
  +
fail fail fail
   
 
Example in Haskell:
 
Example in Haskell:
<example in Haskell>
+
> table2 (\a b -> a `and'` (a `or'` not b))
  +
True True True
  +
True False True
  +
False True False
  +
False False False
 
</pre>
 
</pre>
   
 
Solution:
 
Solution:
 
<haskell>
 
<haskell>
<solution in haskell>
+
-- functions as in solution 46
  +
infixl 4 `or'`
  +
infixl 6 `and'`
  +
-- "not" has fixity 9 by default
 
</haskell>
 
</haskell>
   
<description of implementation>
+
Java operator precedence (descending) as far as I could fathom it:
  +
<pre>
  +
logical not
  +
equality
  +
and
  +
xor
  +
or
  +
</pre>
  +
  +
Using "not" as a non-operator is a little evil, but then again these problems were designed for languages other than haskell :)
 
 
 
== Problem 48 ==
 
== 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>
 
<pre>
 
Example:
 
Example:
<example in lisp>
+
* (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:
 
Example in Haskell:
<example in Haskell>
+
> table3 (\a b c -> a `and'` (b `or'` c) `equ'` a `and'` b `or'` a `and'` c)
  +
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
 
</pre>
 
</pre>
   
 
Solution:
 
Solution:
 
<haskell>
 
<haskell>
<solution in haskell>
+
-- functions as in solution 46
  +
infixl 4 `or'`
  +
infixl 4 `nor'`
  +
infixl 5 `xor'`
  +
infixl 6 `and'`
  +
infixl 6 `nand'`
  +
infixl 3 `equ'` -- was 7, changing it to 3 got me the same results as in the original question :(
  +
  +
table3 :: (Bool -> Bool -> Bool -> Bool) -> IO ()
  +
table3 f = putStrLn . unlines $ [show a ++ " " ++ show b ++ " " ++ show c ++ " " ++ show (f a b c)
  +
| a <- [True, False], b <- [True, False], c <- [True, False]]
 
</haskell>
 
</haskell>
   
<description of implementation>
+
Using individual table functions for different numbers of variables is even more ugly, but anything else would be a bit of a pain in haskell AFAIK.
 
 
 
== Problem 49 ==
 
== Problem 49 ==

Revision as of 03:39, 13 December 2006

Back to 99 Haskell exercises


These are Haskell translations of Ninety Nine Lisp 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.

1 Logic and Codes

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:
> table2 (\a b -> (and' a (or' a b))
True True True
True False True
False True False
False False False

Solution:

not' :: Bool -> Bool
not' True  = False
not' False = True
 
and',or',nor',nand',xor',impl',equ' :: Bool -> Bool -> Bool
and' True True = True
and' _    _    = False
 
or' True _    = True
or' _    True = True
or' _    _    = False
 
nor'  a b = not' $ or'  a b
nand' a b = not' $ and' a b
 
xor' True  False = True
xor' False True  = True
xor' _     _     = False
 
impl' a b = (not' a) `or'` b
 
equ' True  True  = True
equ' False False = True
equ' _     _     = False
 
table2 :: (Bool -> Bool -> Bool) -> IO ()
table2 f = putStrLn . unlines $ [show a ++ " " ++ show b ++ " " ++ show (f a b)
                                | a <- [True, False], b <- [True, False]]

The implementations of the logic functions are quite verbose and can be shortened in places (like "equ' = (==)").

The table function in Lisp supposedly uses Lisp's symbol handling to substitute variables on the fly in the expression. I chose passing a binary function instead because parsing an expression would be more verbose in haskell than it is in Lisp. Template Haskell could also be used :)

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

Solution:

-- functions as in solution 46
infixl 4 `or'`
infixl 6 `and'`
-- "not" has fixity 9 by default

Java operator precedence (descending) as far as I could fathom it:

logical not
equality
and
xor
or

Using "not" as a non-operator is a little evil, but then again these problems were designed for languages other than haskell :)

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:
> table3 (\a b c -> a `and'` (b `or'` c) `equ'` a `and'` b `or'` a `and'` c)
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

Solution:

-- functions as in solution 46
infixl 4 `or'`
infixl 4 `nor'`
infixl 5 `xor'`
infixl 6 `and'`
infixl 6 `nand'`
infixl 3 `equ'` -- was 7, changing it to 3 got me the same results as in the original question :(
 
table3 :: (Bool -> Bool -> Bool -> Bool) -> IO ()
table3 f = putStrLn . unlines $ [show a ++ " " ++ show b ++ " " ++ show c ++ " " ++ show (f a b c)
                                | a <- [True, False], b <- [True, False], c <- [True, False]]

Using individual table functions for different numbers of variables is even more ugly, but anything else would be a bit of a pain in haskell AFAIK.

5 Problem 49

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 Haskell:
P49> gray 3
["000","001","011","010","110","111","101","100"]

Solution:

gray :: Int -> [String]
gray 1     = ["0", "1"]
gray (n+1) = let xs = gray n in map ('0':) xs ++ map ('1':) (reverse xs)

It seems that the gray code can be recursively defined in the way that for determining the gray code of n we take the gray code of n-1, prepend a 0 to each word, take the gray code for n-1 again, reverse it and perpend a 1 to each word. At last we have to append these two lists. (Wikipedia seems to approve this.)

Instead of the equation for
gray 1 = ...
we could also use
gray 0     = [""]

what leads to the same results.


6 Problem 50

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>