# 99 questions/11 to 20

### From HaskellWiki

(Added a solution to Problem 18) |
(moved solutions to subpages of 99 questions/Solutions, reformatted code examples) |
||

Line 5: | Line 5: | ||

== Problem 11 == | == Problem 11 == | ||

− | (*) Modified run-length encoding. | + | (*) Modified run-length encoding. |

+ | |||

Modify the result of problem 10 in such a way that if an element has no duplicates it is simply copied into the result list. Only elements with duplicates are transferred as (N E) lists. | Modify the result of problem 10 in such a way that if an element has no duplicates it is simply copied into the result list. Only elements with duplicates are transferred as (N E) lists. | ||

− | |||

Example: | Example: | ||

+ | |||

+ | <pre> | ||

* (encode-modified '(a a a a b c c a a d e e e e)) | * (encode-modified '(a a a a b c c a a d e e e e)) | ||

((4 A) B (2 C) (2 A) D (4 E)) | ((4 A) B (2 C) (2 A) D (4 E)) | ||

+ | </pre> | ||

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

− | |||

− | |||

− | |||

− | |||

<haskell> | <haskell> | ||

− | + | P11> encodeModified "aaaabccaadeeee" | |

− | + | [Multiple 4 'a',Single 'b',Multiple 2 'c',Multiple 2 'a',Single 'd',Multiple 4 'e'] | |

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

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

− | + | [[99 questions/Solutions/11 | Solutions]] | |

− | |||

== Problem 12 == | == Problem 12 == | ||

− | (**) Decode a run-length encoded list. | + | (**) Decode a run-length encoded list. |

+ | |||

Given a run-length code list generated as specified in problem 11. Construct its uncompressed version. | Given a run-length code list generated as specified in problem 11. Construct its uncompressed version. | ||

− | |||

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

+ | |||

+ | <haskell> | ||

P12> decodeModified [Multiple 4 'a',Single 'b',Multiple 2 'c',Multiple 2 'a',Single 'd',Multiple 4 'e'] | P12> decodeModified [Multiple 4 'a',Single 'b',Multiple 2 'c',Multiple 2 'a',Single 'd',Multiple 4 'e'] | ||

"aaaabccaadeeee" | "aaaabccaadeeee" | ||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

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

− | + | [[99 questions/Solutions/12 | Solutions]] | |

+ | |||

== Problem 13 == | == Problem 13 == | ||

(**) Run-length encoding of a list (direct solution). | (**) Run-length encoding of a list (direct solution). | ||

+ | |||

Implement the so-called run-length encoding data compression method directly. I.e. don't explicitly create the sublists containing the duplicates, as in problem 9, but only count them. As in problem P11, simplify the result list by replacing the singleton lists (1 X) by X. | Implement the so-called run-length encoding data compression method directly. I.e. don't explicitly create the sublists containing the duplicates, as in problem 9, but only count them. As in problem P11, simplify the result list by replacing the singleton lists (1 X) by X. | ||

− | |||

Example: | Example: | ||

+ | |||

+ | <pre> | ||

* (encode-direct '(a a a a b c c a a d e e e e)) | * (encode-direct '(a a a a b c c a a d e e e e)) | ||

((4 A) B (2 C) (2 A) D (4 E)) | ((4 A) B (2 C) (2 A) D (4 E)) | ||

+ | </pre> | ||

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

+ | |||

+ | <haskell> | ||

P13> encodeDirect "aaaabccaadeeee" | P13> encodeDirect "aaaabccaadeeee" | ||

[Multiple 4 'a',Single 'b',Multiple 2 'c',Multiple 2 'a',Single 'd',Multiple 4 'e'] | [Multiple 4 'a',Single 'b',Multiple 2 'c',Multiple 2 'a',Single 'd',Multiple 4 'e'] | ||

− | </ | + | </haskell> |

− | + | [[99 questions/Solutions/13 | Solutions]] | |

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

== Problem 14 == | == Problem 14 == | ||

(*) Duplicate the elements of a list. | (*) Duplicate the elements of a list. | ||

+ | |||

+ | Example: | ||

<pre> | <pre> | ||

− | |||

* (dupli '(a b c c d)) | * (dupli '(a b c c d)) | ||

(A A B B C C C C D D) | (A A B B C C C C D D) | ||

+ | </pre> | ||

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

+ | |||

+ | <haskell> | ||

> dupli [1, 2, 3] | > dupli [1, 2, 3] | ||

[1,1,2,2,3,3] | [1,1,2,2,3,3] | ||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

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

− | + | [[99 questions/Solutions/14 | Solutions]] | |

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

== Problem 15 == | == Problem 15 == | ||

(**) Replicate the elements of a list a given number of times. | (**) Replicate the elements of a list a given number of times. | ||

+ | |||

+ | Example: | ||

<pre> | <pre> | ||

− | |||

* (repli '(a b c) 3) | * (repli '(a b c) 3) | ||

(A A A B B B C C C) | (A A A B B B C C C) | ||

+ | </pre> | ||

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

− | |||

− | |||

− | |||

− | |||

<haskell> | <haskell> | ||

− | repli | + | > repli "abc" 3 |

− | + | "aaabbbccc" | |

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

− | + | [[99 questions/Solutions/15 | Solutions]] | |

− | + | ||

− | + | ||

− | + | ||

== Problem 16 == | == Problem 16 == | ||

+ | |||

(**) Drop every N'th element from a list. | (**) Drop every N'th element from a list. | ||

− | |||

Example: | Example: | ||

+ | |||

+ | <pre> | ||

* (drop '(a b c d e f g h i k) 3) | * (drop '(a b c d e f g h i k) 3) | ||

(A B D E G H K) | (A B D E G H K) | ||

+ | </pre> | ||

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

+ | |||

+ | <haskell> | ||

*Main> dropEvery "abcdefghik" 3 | *Main> dropEvery "abcdefghik" 3 | ||

"abdeghk" | "abdeghk" | ||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

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

− | + | [[99 questions/Solutions/16 | Solutions]] | |

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

== Problem 17 == | == Problem 17 == | ||

Line 213: | Line 133: | ||

Do not use any predefined predicates. | Do not use any predefined predicates. | ||

+ | |||

+ | Example: | ||

<pre> | <pre> | ||

− | |||

* (split '(a b c d e f g h i k) 3) | * (split '(a b c d e f g h i k) 3) | ||

( (A B C) (D E F G H I K)) | ( (A B C) (D E F G H I K)) | ||

+ | </pre> | ||

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

+ | |||

+ | <haskell> | ||

*Main> split "abcdefghik" 3 | *Main> split "abcdefghik" 3 | ||

("abc", "defghik") | ("abc", "defghik") | ||

− | |||

− | |||

− | |||

− | |||

− | |||

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

− | + | [[99 questions/Solutions/17 | Solutions]] | |

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

== Problem 18 == | == Problem 18 == | ||

Line 253: | Line 156: | ||

Given two indices, i and k, the slice is the list containing the elements between the i'th and k'th element of the original list (both limits included). Start counting the elements with 1. | Given two indices, i and k, the slice is the list containing the elements between the i'th and k'th element of the original list (both limits included). Start counting the elements with 1. | ||

+ | |||

+ | Example: | ||

<pre> | <pre> | ||

− | |||

* (slice '(a b c d e f g h i k) 3 7) | * (slice '(a b c d e f g h i k) 3 7) | ||

(C D E F G) | (C D E F G) | ||

+ | </pre> | ||

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

+ | |||

+ | <haskell> | ||

*Main> slice ['a','b','c','d','e','f','g','h','i','k'] 3 7 | *Main> slice ['a','b','c','d','e','f','g','h','i','k'] 3 7 | ||

"cdefg" | "cdefg" | ||

− | |||

− | |||

− | |||

− | |||

− | |||

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

− | + | [[99 questions/Solutions/18 | Solutions]] | |

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | + | ||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

== Problem 19 == | == Problem 19 == | ||

Line 295: | Line 179: | ||

Hint: Use the predefined functions length and (++). | Hint: Use the predefined functions length and (++). | ||

+ | |||

+ | Examples: | ||

<pre> | <pre> | ||

− | |||

* (rotate '(a b c d e f g h) 3) | * (rotate '(a b c d e f g h) 3) | ||

(D E F G H A B C) | (D E F G H A B C) | ||

Line 303: | Line 188: | ||

* (rotate '(a b c d e f g h) -2) | * (rotate '(a b c d e f g h) -2) | ||

(G H A B C D E F) | (G H A B C D E F) | ||

+ | </pre> | ||

Examples in Haskell: | Examples in Haskell: | ||

+ | |||

+ | <haskell> | ||

*Main> rotate ['a','b','c','d','e','f','g','h'] 3 | *Main> rotate ['a','b','c','d','e','f','g','h'] 3 | ||

"defghabc" | "defghabc" | ||

Line 310: | Line 198: | ||

*Main> rotate ['a','b','c','d','e','f','g','h'] (-2) | *Main> rotate ['a','b','c','d','e','f','g','h'] (-2) | ||

"ghabcdef" | "ghabcdef" | ||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

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

− | + | [[99 questions/Solutions/19 | Solutions]] | |

− | + | ||

− | + | ||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

== Problem 20 == | == Problem 20 == | ||

Line 352: | Line 208: | ||

Example in Prolog: | Example in Prolog: | ||

+ | |||

<pre> | <pre> | ||

?- remove_at(X,[a,b,c,d],2,R). | ?- remove_at(X,[a,b,c,d],2,R). | ||

Line 359: | Line 216: | ||

Example in Lisp: | Example in Lisp: | ||

+ | |||

<pre> | <pre> | ||

* (remove-at '(a b c d) 2) | * (remove-at '(a b c d) 2) | ||

(A C D) | (A C D) | ||

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

+ | |||

(Note that this only returns the residue list, while the Prolog version also returns the deleted element.) | (Note that this only returns the residue list, while the Prolog version also returns the deleted element.) | ||

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

− | < | + | |

+ | <haskell> | ||

*Main> removeAt 1 "abcd" | *Main> removeAt 1 "abcd" | ||

('b',"acd") | ('b',"acd") | ||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

− | |||

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

− | + | [[99 questions/Solutions/20 | Solutions]] | |

− | + | ||

− | + | ||

− | |||

− | |||

− | |||

− | |||

− | |||

[[Category:Tutorials]] | [[Category:Tutorials]] |

## Revision as of 15:51, 13 July 2010

This is part of Ninety-Nine Haskell Problems, based on Ninety-Nine Prolog Problems and Ninety-Nine Lisp Problems.

## 1 Problem 11

(*) Modified run-length encoding.

Modify the result of problem 10 in such a way that if an element has no duplicates it is simply copied into the result list. Only elements with duplicates are transferred as (N E) lists.

Example:

* (encode-modified '(a a a a b c c a a d e e e e)) ((4 A) B (2 C) (2 A) D (4 E))

Example in Haskell:

P11> encodeModified "aaaabccaadeeee" [Multiple 4 'a',Single 'b',Multiple 2 'c',Multiple 2 'a',Single 'd',Multiple 4 'e']

## 2 Problem 12

(**) Decode a run-length encoded list.

Given a run-length code list generated as specified in problem 11. Construct its uncompressed version.

Example in Haskell:

P12> decodeModified [Multiple 4 'a',Single 'b',Multiple 2 'c',Multiple 2 'a',Single 'd',Multiple 4 'e'] "aaaabccaadeeee"

## 3 Problem 13

(**) Run-length encoding of a list (direct solution).

Implement the so-called run-length encoding data compression method directly. I.e. don't explicitly create the sublists containing the duplicates, as in problem 9, but only count them. As in problem P11, simplify the result list by replacing the singleton lists (1 X) by X.

Example:

* (encode-direct '(a a a a b c c a a d e e e e)) ((4 A) B (2 C) (2 A) D (4 E))

Example in Haskell:

P13> encodeDirect "aaaabccaadeeee" [Multiple 4 'a',Single 'b',Multiple 2 'c',Multiple 2 'a',Single 'd',Multiple 4 'e']

## 4 Problem 14

(*) Duplicate the elements of a list.

Example:

* (dupli '(a b c c d)) (A A B B C C C C D D)

Example in Haskell:

> dupli [1, 2, 3] [1,1,2,2,3,3]

## 5 Problem 15

(**) Replicate the elements of a list a given number of times.

Example:

* (repli '(a b c) 3) (A A A B B B C C C)

Example in Haskell:

> repli "abc" 3 "aaabbbccc"

## 6 Problem 16

(**) Drop every N'th element from a list.

Example:

* (drop '(a b c d e f g h i k) 3) (A B D E G H K)

Example in Haskell:

*Main> dropEvery "abcdefghik" 3 "abdeghk"

## 7 Problem 17

(*) Split a list into two parts; the length of the first part is given.

Do not use any predefined predicates.

Example:

* (split '(a b c d e f g h i k) 3) ( (A B C) (D E F G H I K))

Example in Haskell:

*Main> split "abcdefghik" 3 ("abc", "defghik")

## 8 Problem 18

(**) Extract a slice from a list.

Given two indices, i and k, the slice is the list containing the elements between the i'th and k'th element of the original list (both limits included). Start counting the elements with 1.

Example:

* (slice '(a b c d e f g h i k) 3 7) (C D E F G)

Example in Haskell:

*Main> slice ['a','b','c','d','e','f','g','h','i','k'] 3 7 "cdefg"

## 9 Problem 19

(**) Rotate a list N places to the left.

Hint: Use the predefined functions length and (++).

Examples:

* (rotate '(a b c d e f g h) 3) (D E F G H A B C) * (rotate '(a b c d e f g h) -2) (G H A B C D E F)

Examples in Haskell:

*Main> rotate ['a','b','c','d','e','f','g','h'] 3 "defghabc" *Main> rotate ['a','b','c','d','e','f','g','h'] (-2) "ghabcdef"

## 10 Problem 20

(*) Remove the K'th element from a list.

Example in Prolog:

?- remove_at(X,[a,b,c,d],2,R). X = b R = [a,c,d]

Example in Lisp:

* (remove-at '(a b c d) 2) (A C D)

(Note that this only returns the residue list, while the Prolog version also returns the deleted element.)

Example in Haskell:

*Main> removeAt 1 "abcd" ('b',"acd")