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

From HaskellWiki
Jump to navigation Jump to search
m
Line 221: Line 221:
 
Solution:
 
Solution:
 
<haskell>
 
<haskell>
 
removeAt k xs = take k xs ++ drop (k+1) xs
-- the simplest solution
 
removeAt1 k xs = take k xs ++ drop (k+1) xs
 
 
-- next attempt, the only problem is, this isn't tail recursive
 
removeAt2 _ [] = []
 
removeAt2 0 xs = tail xs
 
removeAt2 k (x:xs) = x : removeAt (k-1) 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.
 
removeAt k xs = removeAcc k xs []
 
where
 
-- 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 (k-1) xs (x:acc)
 
 
</haskell>
 
</haskell>
   
  +
Simply use the take and drop functions from the Prelude to take the list from the start to k and prepend to the list from k+1 to the end.
Three solutions, starting with the simplest and moving to the highest performance.
 
   
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]

Revision as of 08:33, 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 11

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 12

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

Problem 13

<Problem description>

Example:
<example in lisp>

Example in Haskell:
<example in Haskell>

Solution:

<solution in haskell>

<description of implementation>

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]

Solution:

dupli [] = []
dupli (x:xs) = [x,x] ++ dupli(xs)

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"

Solution:

repli :: [a] -> Int -> [a]
repli as n = concatMap (replicate n) as

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> drop = "abcdefghik" 3
"abdeghk"

Solution:

drop xs n = drops xs (n-1) n
drops [] _ _ = []
drops (x:xs) 0 max = drops xs (max-1) max
drops (x:xs) (n+1) max = x:drops xs n max

Here, drops is a helper-function to drop. In drops, there is an index n that counts from max-1 down to 0, and removes the head element each time it hits 0.

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

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

Solution using take and drop:

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


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 i'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

Solution:

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

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"

Solution:

rotate [] _ = []
rotate l 0 = l
rotate (x:xs) (n+1) = rotate (xs ++ [x]) n
rotate l n = rotate l (length l + n)

There are two separate cases:
- If n > 0, move the first element to the end of the list n times.
- If n < 0, convert the problem to the equivalent problem for n > 0 by adding the list's length to n.


Problem 20

Remove the K'th element from a list.

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

Example in Haskell:
*Main> removeAt 1 ['a','b','c','d']
"acd"

Solution:

removeAt k xs = take k xs ++ drop (k+1) xs

Simply use the take and drop functions from the Prelude to take the list from the start to k and prepend to the list from k+1 to the end.