# 99 questions/61 to 69

### From HaskellWiki

m (solution 61) |
m (solution to prob. 61A) |
||

Line 23: | Line 23: | ||

Solution: |
Solution: |
||

<haskell> |
<haskell> |
||

− | data Tree a = Nil | Branch a (Tree a) (Tree a) |
+ | data Tree a = Nil | Branch a (Tree a) (Tree a) deriving (Show, Eq) |

count_leaves Nil = 0 |
count_leaves Nil = 0 |
||

Line 32: | Line 32: | ||

== Problem 61A == |
== Problem 61A == |
||

− | <Problem description> |
+ | Collect the leaves of a binary tree in a list |

+ | |||

+ | A leaf is a node with no successors. Write a predicate leaves/2 to collect them in a list. |
||

<pre> |
<pre> |
||

Example: |
Example: |
||

− | <example in lisp> |
+ | % leaves(T,S) :- S is the list of all leaves of the binary tree T |

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

− | <example in Haskell> |
+ | > leaves (Branch 1 (Branch 2 Nil (Branch 4 Nil Nil)) (Branch 2 Nil Nil)) |

+ | [Branch 4 Nil Nil, Branch 2 Nil Nil] |
||

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

Solution: |
Solution: |
||

<haskell> |
<haskell> |
||

− | <solution in haskell> |
+ | leaves Nil = [] |

+ | leaves b@(Branch a Nil Nil) = [b] |
||

+ | leaves (Branch a left right) = leaves left ++ leaves right |
||

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

− | <description of implementation> |
+ | |

== Problem 62 == |
== Problem 62 == |

## Revision as of 13:27, 13 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 61

Count the leaves of a binary tree

A leaf is a node with no successors. Write a predicate count_leaves/2 to count them.

Example: % count_leaves(T,N) :- the binary tree T has N leaves Example in Haskell: > count_leaves (Branch 1 (Branch 2 Nil (Branch 4 Nil Nil)) (Branch 2 Nil Nil)) 2

Solution:

data Tree a = Nil | Branch a (Tree a) (Tree a) deriving (Show, Eq) count_leaves Nil = 0 count_leaves (Branch a Nil Nil) = 1 count_leaves (Branch a left right) = count_leaves left + count_leaves right

## 2 Problem 61A

Collect the leaves of a binary tree in a list

A leaf is a node with no successors. Write a predicate leaves/2 to collect them in a list.

Example: % leaves(T,S) :- S is the list of all leaves of the binary tree T Example in Haskell: > leaves (Branch 1 (Branch 2 Nil (Branch 4 Nil Nil)) (Branch 2 Nil Nil)) [Branch 4 Nil Nil, Branch 2 Nil Nil]

Solution:

leaves Nil = [] leaves b@(Branch a Nil Nil) = [b] leaves (Branch a left right) = leaves left ++ leaves right

## 3 Problem 62

<Problem description>

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

Solution:

<solution in haskell>

<description of implementation>

## 4 Problem 62B

<Problem description>

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

Solution:

<solution in haskell>

<description of implementation>

## 5 Problem 63

<Problem description>

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

Solution:

<solution in haskell>

<description of implementation>

## 6 Problem 64

<Problem description>

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

Solution:

<solution in haskell>

<description of implementation>

## 7 Problem 65

<Problem description>

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

Solution:

<solution in haskell>

<description of implementation>

## 8 Problem 66

<Problem description>

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

Solution:

<solution in haskell>

<description of implementation>

## 9 Problem 67

<Problem description>

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

Solution:

<solution in haskell>

<description of implementation>

## 10 Problem 68

<Problem description>

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

Solution:

<solution in haskell>

<description of implementation>

## 11 Problem 69

<Problem description>

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

Solution:

<solution in haskell>

<description of implementation>