Difference between revisions of "99 questions/11 to 20"

From HaskellWiki
Jump to navigation Jump to search
 
m (unify ghci prompts)
(66 intermediate revisions by 30 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
   
These are Haskell translations of [http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html Ninety Nine Lisp Problems].
+
This is part of [[H-99:_Ninety-Nine_Haskell_Problems|Ninety-Nine Haskell Problems]], based on [https://prof.ti.bfh.ch/hew1/informatik3/prolog/p-99/ Ninety-Nine Prolog Problems] and [http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html Ninety-Nine Lisp Problems].
  +
  +
== Problem 11 ==
   
  +
(*) Modified run-length encoding.
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.
 
   
  +
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.
 
== Problem 11 ==
 
   
  +
Example:
<Problem description>
 
   
 
<pre>
 
<pre>
  +
* (encode-modified '(a a a a b c c a a d e e e e))
Example:
 
  +
((4 A) B (2 C) (2 A) D (4 E))
<example in lisp>
 
  +
</pre>
   
 
Example in Haskell:
 
Example in Haskell:
<example in Haskell>
 
</pre>
 
   
Solution:
 
 
<haskell>
 
<haskell>
  +
λ> encodeModified "aaaabccaadeeee"
<solution in haskell>
 
  +
[Multiple 4 'a',Single 'b',Multiple 2 'c',
  +
Multiple 2 'a',Single 'd',Multiple 4 'e']
 
</haskell>
 
</haskell>
   
  +
[[99 questions/Solutions/11 | Solutions]]
<description of implementation>
 
  +
 
 
== Problem 12 ==
 
== Problem 12 ==
   
  +
(**) Decode a run-length encoded list.
<Problem description>
 
   
  +
Given a run-length code list generated as specified in problem 11. Construct its uncompressed version.
<pre>
 
Example:
 
<example in lisp>
 
   
 
Example in Haskell:
 
Example in Haskell:
<example in Haskell>
 
</pre>
 
   
Solution:
 
 
<haskell>
 
<haskell>
  +
λ> decodeModified
<solution in haskell>
 
  +
[Multiple 4 'a',Single 'b',Multiple 2 'c',
  +
Multiple 2 'a',Single 'd',Multiple 4 'e']
  +
"aaaabccaadeeee"
 
</haskell>
 
</haskell>
   
  +
[[99 questions/Solutions/12 | Solutions]]
<description of implementation>
 
  +
 
 
== Problem 13 ==
 
== Problem 13 ==
   
  +
(**) Run-length encoding of a list (direct solution).
<Problem description>
 
  +
  +
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.
   
<pre>
 
 
Example:
 
Example:
<example in lisp>
 
   
  +
<pre>
Example in Haskell:
 
  +
* (encode-direct '(a a a a b c c a a d e e e e))
<example in Haskell>
 
  +
((4 A) B (2 C) (2 A) D (4 E))
 
</pre>
 
</pre>
   
  +
Example in Haskell:
Solution:
 
  +
 
<haskell>
 
<haskell>
  +
λ> encodeDirect "aaaabccaadeeee"
<solution in haskell>
 
  +
[Multiple 4 'a',Single 'b',Multiple 2 'c',
  +
Multiple 2 'a',Single 'd',Multiple 4 'e']
 
</haskell>
 
</haskell>
   
  +
[[99 questions/Solutions/13 | Solutions]]
<description of implementation>
 
  +
 
 
== Problem 14 ==
 
== Problem 14 ==
   
  +
(*) Duplicate the elements of a list.
<Problem description>
 
   
<pre>
 
 
Example:
 
Example:
<example in lisp>
 
   
  +
<pre>
Example in Haskell:
 
  +
* (dupli '(a b c c d))
<example in Haskell>
 
  +
(A A B B C C C C D D)
 
</pre>
 
</pre>
   
  +
Example in Haskell:
Solution:
 
  +
 
<haskell>
 
<haskell>
  +
λ> dupli [1, 2, 3]
<solution in haskell>
 
  +
[1,1,2,2,3,3]
 
</haskell>
 
</haskell>
   
  +
[[99 questions/Solutions/14 | Solutions]]
<description of implementation>
 
  +
 
  +
 
== Problem 15 ==
 
== Problem 15 ==
   
  +
(**) Replicate the elements of a list a given number of times.
<Problem description>
 
   
<pre>
 
 
Example:
 
Example:
<example in lisp>
 
   
  +
<pre>
Example in Haskell:
 
  +
* (repli '(a b c) 3)
<example in Haskell>
 
  +
(A A A B B B C C C)
 
</pre>
 
</pre>
   
  +
Example in Haskell:
Solution:
 
  +
 
<haskell>
 
<haskell>
  +
λ> repli "abc" 3
<solution in haskell>
 
  +
"aaabbbccc"
 
</haskell>
 
</haskell>
   
  +
[[99 questions/Solutions/15 | Solutions]]
<description of implementation>
 
  +
 
  +
 
== Problem 16 ==
 
== Problem 16 ==
   
  +
(**) Drop every N'th element from a list.
<Problem description>
 
   
<pre>
 
 
Example:
 
Example:
<example in lisp>
 
   
  +
<pre>
Example in Haskell:
 
  +
* (drop '(a b c d e f g h i k) 3)
<example in Haskell>
 
  +
(A B D E G H K)
 
</pre>
 
</pre>
   
  +
Example in Haskell:
Solution:
 
  +
 
<haskell>
 
<haskell>
  +
λ> dropEvery "abcdefghik" 3
<solution in haskell>
 
  +
"abdeghk"
 
</haskell>
 
</haskell>
   
  +
[[99 questions/Solutions/16 | Solutions]]
<description of implementation>
 
  +
 
 
 
== Problem 17 ==
 
== Problem 17 ==
   
  +
(*) Split a list into two parts; the length of the first part is given.
<Problem description>
 
  +
  +
Do not use any predefined predicates.
   
<pre>
 
 
Example:
 
Example:
<example in lisp>
 
   
  +
<pre>
Example in Haskell:
 
  +
* (split '(a b c d e f g h i k) 3)
<example in Haskell>
 
  +
( (A B C) (D E F G H I K))
 
</pre>
 
</pre>
   
  +
Example in Haskell:
Solution:
 
  +
 
<haskell>
 
<haskell>
  +
λ> split "abcdefghik" 3
<solution in haskell>
 
  +
("abc", "defghik")
 
</haskell>
 
</haskell>
   
  +
[[99 questions/Solutions/17 | Solutions]]
<description of implementation>
 
  +
 
  +
 
== Problem 18 ==
 
== Problem 18 ==
   
  +
(**) Extract a slice from a list.
<Problem description>
 
  +
  +
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.
   
<pre>
 
 
Example:
 
Example:
<example in lisp>
 
   
  +
<pre>
Example in Haskell:
 
  +
* (slice '(a b c d e f g h i k) 3 7)
<example in Haskell>
 
  +
(C D E F G)
 
</pre>
 
</pre>
   
  +
Example in Haskell:
Solution:
 
  +
 
<haskell>
 
<haskell>
  +
λ> slice ['a','b','c','d','e','f','g','h','i','k'] 3 7
<solution in haskell>
 
  +
"cdefg"
 
</haskell>
 
</haskell>
   
  +
[[99 questions/Solutions/18 | Solutions]]
<description of implementation>
 
  +
 
  +
 
== Problem 19 ==
 
== Problem 19 ==
   
  +
(**) Rotate a list N places to the left.
<Problem description>
 
  +
  +
Hint: Use the predefined functions length and (++).
  +
  +
Examples:
   
 
<pre>
 
<pre>
  +
* (rotate '(a b c d e f g h) 3)
Example:
 
  +
(D E F G H A B C)
<example in lisp>
 
   
  +
* (rotate '(a b c d e f g h) -2)
Example in Haskell:
 
  +
(G H A B C D E F)
<example in Haskell>
 
 
</pre>
 
</pre>
   
  +
Examples in Haskell:
Solution:
 
  +
 
<haskell>
 
<haskell>
  +
λ> rotate ['a','b','c','d','e','f','g','h'] 3
<solution in haskell>
 
  +
"defghabc"
  +
  +
λ> rotate ['a','b','c','d','e','f','g','h'] (-2)
  +
"ghabcdef"
 
</haskell>
 
</haskell>
   
  +
[[99 questions/Solutions/19 | Solutions]]
<description of implementation>
 
  +
 
  +
 
== Problem 20 ==
 
== Problem 20 ==
   
  +
(*) Remove the K'th element from a list.
<Problem description>
 
  +
  +
Example in Prolog:
   
 
<pre>
 
<pre>
  +
?- remove_at(X,[a,b,c,d],2,R).
Example:
 
  +
X = b
<example in lisp>
 
  +
R = [a,c,d]
  +
</pre>
   
Example in Haskell:
+
Example in Lisp:
  +
<example in Haskell>
 
  +
<pre>
  +
* (remove-at '(a b c d) 2)
  +
(A C D)
 
</pre>
 
</pre>
   
  +
(Note that this only returns the residue list, while the Prolog version also returns the deleted element.)
Solution:
 
  +
  +
Example in Haskell:
  +
 
<haskell>
 
<haskell>
  +
λ> removeAt 2 "abcd"
<solution in haskell>
 
  +
('b',"acd")
 
</haskell>
 
</haskell>
   
  +
[[99 questions/Solutions/20 | Solutions]]
<description of implementation>
 
  +
   
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]

Revision as of 08:38, 8 February 2019


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

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:

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

Solutions

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:

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

Solutions

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:

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

Solutions

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]

Solutions


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"

Solutions


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:

λ> dropEvery "abcdefghik" 3
"abdeghk"

Solutions


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:

λ> split "abcdefghik" 3
("abc", "defghik")

Solutions


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:

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

Solutions


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:

λ> rotate ['a','b','c','d','e','f','g','h'] 3
"defghabc"

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

Solutions


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:

λ> removeAt 2 "abcd"
('b',"acd")

Solutions