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

(→Problem 26) |
|||

Line 103: | Line 103: | ||

== Problem 26 == |
== Problem 26 == |
||

− | <Problem description> |
||

+ | (**) 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. |
||

<pre> |
<pre> |
||

Example: |
Example: |
||

− | <example in lisp> |
||

+ | * (combinations 3 '(a b c d e f)) |
||

+ | ((A B C) (A B D) (A B E) ... ) |
||

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

− | <example in Haskell> |
||

+ | > combinations 3 "abcdef" |
||

+ | ["abc","abd","abe",...] |
||

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

Solution: |
Solution: |
||

<haskell> |
<haskell> |
||

− | <solution in haskell> |
||

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

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

− | <description of implementation> |
||

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

− | |||

+ | |||

== Problem 27 == |
== Problem 27 == |
||

## Revision as of 08:34, 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

<Problem description>

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

Solution:

```
<solution in haskell>
```

<description of implementation>

## 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>