Personal tools

99 questions/54A to 60

From HaskellWiki

< 99 questions(Difference between revisions)
Jump to: navigation, search
m (99 questions/51 to 60 moved to 99 questions/54A to 60)
(Solve P54A)
Line 8: Line 8:
 
== Problem 54A ==
 
== Problem 54A ==
   
<Problem description>
+
(*) Check whether a given term represents a binary tree
   
  +
Example:
 
<pre>
 
<pre>
Example:
+
* (istree (a (b nil nil) nil))
<example in lisp>
+
T
+
* (istree (a (b nil nil)))
Example in Haskell:
+
NIL
<example in Haskell>
 
 
</pre>
 
</pre>
   
Solution:
+
The typical solution in Haskell is to introduce an algebraic data type:
 
<haskell>
 
<haskell>
<solution in haskell>
+
data Tree a = Leaf | Branch a (Tree a) (Tree a)
 
</haskell>
 
</haskell>
   
<description of implementation>
+
The type system ensures that all terms of type <haskell>Tree a</haskell> are binary trees. Hence, it is redundant to introduce a predicate to check this property.
 
 
 
== Problem 55 ==
 
== Problem 55 ==

Revision as of 20:09, 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 54A

(*) Check whether a given term represents a binary tree

Example:

* (istree (a (b nil nil) nil))
T
* (istree (a (b nil nil)))
NIL

The typical solution in Haskell is to introduce an algebraic data type:

data Tree a = Leaf | Branch a (Tree a) (Tree a)
The type system ensures that all terms of type
Tree a
are binary trees. Hence, it is redundant to introduce a predicate to check this property.

2 Problem 55

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

3 Problem 56

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

4 Problem 57

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

5 Problem 58

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

6 Problem 59

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

7 Problem 60

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>