Difference between revisions of "99 questions/21 to 28"

From HaskellWiki
Jump to navigation Jump to search
(Problem 22)
Line 27: Line 27:
 
== Problem 22 ==
 
== Problem 22 ==
   
  +
Create a list containing all integers within a given range.
<Problem description>
 
   
 
<pre>
 
<pre>
 
Example:
 
Example:
  +
Example:
<example in lisp>
 
  +
* (range 4 9)
  +
(4 5 6 7 8 9)
   
 
Example in Haskell:
 
Example in Haskell:
  +
Prelude> [4..9]
<example in Haskell>
 
  +
[4,5,6,7,8,9]
 
</pre>
 
</pre>
   
 
Solution:
 
Solution:
 
<haskell>
 
<haskell>
  +
range x y = [x..y]
<solution in haskell>
 
 
</haskell>
 
</haskell>
   
  +
Since there's already syntactic sugar for ranges, there's usually no reason to define a function like 'range' in Haskell.
<description of implementation>
 
 
 
 
== Problem 23 ==
 
== Problem 23 ==

Revision as of 08:46, 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.


Problem 21

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 22

Create a list containing all integers within a given range.

Example:
    Example:
    * (range 4 9)
    (4 5 6 7 8 9)

Example in Haskell:
Prelude> [4..9]
[4,5,6,7,8,9]

Solution:

range x y = [x..y]

Since there's already syntactic sugar for ranges, there's usually no reason to define a function like 'range' in Haskell.

Problem 23

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 24

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 25

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 26

(**) Generate the combinations of K distinct objects chosen from the N elements of a list In how many ways can a committee of 3 be chosen from a group of 12 people? We all know that there are C(12,3) = 220 possibilities (C(N,K) denotes the well-known binomial coefficients). For pure mathematicians, this result may be great. But we want to really generate all the possibilities in a list.

Example:
* (combinations 3 '(a b c d e f))
((A B C) (A B D) (A B E) ... )

Example in Haskell:
> combinations 3 "abcdef"
["abc","abd","abe",...]

Solution:

import Data.List (tails)

combinations :: Int -> [a] -> [[a]]
combinations 0 _  = do return []
combinations n xs = do y:xs' <- tails xs
                       ys <- combinations (n-1) xs'
                       return (y:ys)

This implementation uses the (lazy) list monad to express nondeterministic choice

Problem 27

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 28

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 29

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 30

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>