# 99 questions/46 to 50

### From HaskellWiki

(P49 solved.) |
|||

Line 160: | Line 160: | ||

== Problem 49 == |
== Problem 49 == |
||

− | <Problem description> |
+ | An n-bit Gray code is a sequence of n-bit strings constructed according to certain rules. For example, |

− | |||

<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´]. |
||

+ | </pre> |
||

+ | 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? |
||

+ | |||

+ | <pre> |
||

Example in Haskell: |
Example in Haskell: |
||

− | <example in Haskell> |
+ | P49> gray 3 |

+ | ["000","001","011","010","110","111","101","100"] |
||

</pre> |
</pre> |
||

Solution: |
Solution: |
||

<haskell> |
<haskell> |
||

− | <solution in haskell> |
+ | gray :: Int -> [String] |

+ | gray 1 = ["0", "1"] |
||

+ | gray (n+1) = let xs = gray n in map ('0':) xs ++ map ('1':) (reverse xs) |
||

+ | </haskell> |
||

+ | |||

+ | 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 <hask>gray 1 = ...</hask> we could also use |
||

+ | <haskell> |
||

+ | gray 0 = [""] |
||

</haskell> |
</haskell> |
||

+ | what leads to the same results. |
||

− | <description of implementation> |
||

== Problem 50 == |
== Problem 50 == |

## Revision as of 13:35, 12 December 2006

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 Problem 41

<Problem description>

Example: <example in lisp> Example in Haskell: <example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

## 2 Problem 42

<Problem description>

Example: <example in lisp> Example in Haskell: <example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

## 3 Problem 43

<Problem description>

Example: <example in lisp> Example in Haskell: <example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

## 4 Problem 44

<Problem description>

Example: <example in lisp> Example in Haskell: <example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

## 5 Problem 45

<Problem description>

Example: <example in lisp> Example in Haskell: <example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

## 6 Problem 46

<Problem description>

Example: <example in lisp> Example in Haskell: <example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

## 7 Problem 47

<Problem description>

Example: <example in lisp> Example in Haskell: <example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

## 8 Problem 48

<Problem description>

Example: <example in lisp> Example in Haskell: <example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

## 9 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 forgray 0 = [""]

what leads to the same results.

## 10 Problem 50

<Problem description>

Example: <example in lisp> Example in Haskell: <example in Haskell>

Solution:

<solution in haskell>

<description of implementation>