Difference between revisions of "99 questions/11 to 20"
(→Problem 15) 
m (unify ghci prompts) 

(57 intermediate revisions by 28 users not shown)  
Line 1:  Line 1:  
__NOTOC__ 
__NOTOC__ 

−  +  This is part of [[H99:_NinetyNine_Haskell_ProblemsNinetyNine Haskell Problems]], based on [https://prof.ti.bfh.ch/hew1/informatik3/prolog/p99/ NinetyNine Prolog Problems] and [http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L99_NinetyNine_Lisp_Problems.html NinetyNine Lisp Problems]. 

+  
+  == Problem 11 == 

−  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. 

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

−  == Problem 11 == 

−  <Problem description> 

+  Example: 

<pre> 
<pre> 

−  Example: 

+  * (encodemodified '(a a a a b c c a a d e e e e)) 

−  <example in lisp> 

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

+  </pre> 

Example in Haskell: 
Example in Haskell: 

−  <example in Haskell> 

−  </pre> 

−  Solution: 

<haskell> 
<haskell> 

−  <solution in haskell> 

+  λ> encodeModified "aaaabccaadeeee" 

+  [Multiple 4 'a',Single 'b',Multiple 2 'c', 

+  Multiple 2 'a',Single 'd',Multiple 4 'e'] 

</haskell> 
</haskell> 

−  <description of implementation> 

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

−  
+  
== Problem 12 == 
== Problem 12 == 

−  <Problem description> 

+  (**) Decode a runlength encoded list. 

−  <pre> 

+  Given a runlength code list generated as specified in problem 11. Construct its uncompressed version. 

−  Example: 

−  <example in lisp> 

Example in Haskell: 
Example in Haskell: 

−  <example in Haskell> 

−  </pre> 

−  Solution: 

<haskell> 
<haskell> 

−  <solution in haskell> 

+  λ> decodeModified 

+  [Multiple 4 'a',Single 'b',Multiple 2 'c', 

+  Multiple 2 'a',Single 'd',Multiple 4 'e'] 

+  "aaaabccaadeeee" 

</haskell> 
</haskell> 

−  <description of implementation> 

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

−  
+  
== Problem 13 == 
== Problem 13 == 

−  <Problem description> 

+  (**) Runlength encoding of a list (direct solution). 

+  
+  Implement the socalled runlength 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> 

−  Example in Haskell: 

+  <pre> 

−  <example in Haskell> 

+  * (encodedirect '(a a a a b c c a a d e e e e)) 

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

</pre> 
</pre> 

−  Solution: 

+  Example in Haskell: 

+  
<haskell> 
<haskell> 

−  <solution in haskell> 

+  λ> encodeDirect "aaaabccaadeeee" 

+  [Multiple 4 'a',Single 'b',Multiple 2 'c', 

+  Multiple 2 'a',Single 'd',Multiple 4 'e'] 

</haskell> 
</haskell> 

−  <description of implementation> 

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

−  
+  
== Problem 14 == 
== Problem 14 == 

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

−  <pre> 

Example: 
Example: 

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

−  > dupli [1, 2, 3] 

−  [1,1,2,2,3,3] 

−  </pre> 

−  Solution: 

<haskell> 
<haskell> 

−  dupli [ 
+  λ> dupli [1, 2, 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. 
−  <pre> 

Example: 
Example: 

+  
+  <pre> 

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

−  +  (A A A B B B C C C) 

+  </pre> 

Example in Haskell: 
Example in Haskell: 

−  > repli "abc" 3 

−  "aaabbbccc" 

−  </pre> 

−  Solution: 

<haskell> 
<haskell> 

−  repli :: [a] > Int > [a] 

+  λ> repli "abc" 3 

−  repli as n = concatMap (replicate n) as 

+  "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. 

−  <pre> 

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: 

−  *Main> drop = "abcdefghik" 3 

−  "abdeghk" 

−  </pre> 

−  Solution: 

<haskell> 
<haskell> 

−  drop xs n = drops xs (n1) n 

+  λ> dropEvery "abcdefghik" 3 

−  drops [] _ _ = [] 

+  "abdeghk" 

−  drops (x:xs) 0 max = drops xs (max1) max 

−  drops (x:xs) (n+1) max = x:drops xs n max 

</haskell> 
</haskell> 

−  Here, drops is a helperfunction to drop. In drops, there is an index n that counts from max1 down to 0, and removes the head element each time it hits 0. 

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

−  Note that drop is one of the standard Haskell functions, so redefining it is generally not a good idea. 

== Problem 17 == 
== Problem 17 == 

Line 134:  Line 132:  
Do not use any predefined predicates. 
Do not use any predefined predicates. 

−  <pre> 

Example: 
Example: 

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

−  *Main> split "abcdefghik" 3 

−  ("abc", "defghik") 

−  </pre> 

−  Solution using take and drop: 

<haskell> 
<haskell> 

−  split xs n = (take n xs, drop n xs) 

+  λ> split "abcdefghik" 3 

+  ("abc", "defghik") 

</haskell> 
</haskell> 

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

+  
+  
== Problem 18 == 
== Problem 18 == 

(**) Extract a slice from a list. 
(**) 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 
+  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: 

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

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

−  </pre> 

−  Solution: 

<haskell> 
<haskell> 

−  slice xs (i+1) k = take (ki) $ drop i xs 

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

+  "cdefg" 

</haskell> 
</haskell> 

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

+  
+  
== Problem 19 == 
== Problem 19 == 

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

−  <pre> 

Examples: 
Examples: 

+  
+  <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 183:  Line 186:  
* (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: 

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

+  
+  <haskell> 

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

"defghabc" 
"defghabc" 

−  +  λ> rotate ['a','b','c','d','e','f','g','h'] (2) 

"ghabcdef" 
"ghabcdef" 

−  </pre> 

−  
−  Solution: 

−  <haskell> 

−  rotate [] _ = [] 

−  rotate l 0 = l 

−  rotate (x:xs) (n+1) = rotate (xs ++ [x]) n 

−  rotate l n = rotate l (length l + n) 

</haskell> 
</haskell> 

−  There are two separate cases: 

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

−  <br/> If n > 0, move the first element to the end of the list n times. 

−  <br/> If n < 0, convert the problem to the equivalent problem for n > 0 by adding the list's length to n. 

== Problem 20 == 
== Problem 20 == 

−  Remove the K'th element from a list. 
+  (*) Remove the K'th element from a list. 
+  
+  Example in Prolog: 

+  
+  <pre> 

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

+  X = b 

+  R = [a,c,d] 

+  </pre> 

+  
+  Example in Lisp: 

<pre> 
<pre> 

−  Example: 

* (removeat '(a b c d) 2) 
* (removeat '(a b c d) 2) 

(A C D) 
(A C D) 

−  
−  Example in Haskell: 

−  *Main> removeAt 1 ['a','b','c','d'] 

−  "acd" 

</pre> 
</pre> 

−  Solution: 

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

−  <haskell> 

−   the simplest solution 

−  removeAt1 k xs = take k xs ++ drop (k+1) xs 

−   next attempt, the only problem is, this isn't tail recursive 

+  Example in Haskell: 

−  removeAt2 _ [] = [] 

−  removeAt2 0 xs = tail xs 

−  removeAt2 k (x:xs) = x : removeAt (k1) xs 

−   O(n) version, uses the typical solution of defining a "helper" function that has an accumulator parameter to contain the list that's been consumed so far. 

+  <haskell> 

−  removeAt k xs = removeAcc k xs [] 

+  λ> removeAt 2 "abcd" 

−  where 

+  ('b',"acd") 

−   appendRev a b = reverse a ++ b 

−  appendRev = flip $ foldl (flip (:)) 

−  removeAcc _ [] acc = reverse acc 

−  removeAcc 0 xs acc = appendRev acc $ tail xs 

−  removeAcc k (x:xs) acc = removeAcc (k1) xs (x:acc) 

</haskell> 
</haskell> 

−  Three solutions, starting with the simplest and moving to the highest performance. 

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

+  
[[Category:Tutorials]] 
[[Category:Tutorials]] 
Latest revision as of 08:38, 8 February 2019
This is part of NinetyNine Haskell Problems, based on NinetyNine Prolog Problems and NinetyNine Lisp Problems.
Problem 11
(*) Modified runlength 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:
* (encodemodified '(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']
Problem 12
(**) Decode a runlength encoded list.
Given a runlength 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"
Problem 13
(**) Runlength encoding of a list (direct solution).
Implement the socalled runlength 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:
* (encodedirect '(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']
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]
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"
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"
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")
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"
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"
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:
* (removeat '(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")