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

From HaskellWiki
Jump to: navigation, search
(suggest modifying problem 15)
Line 7: Line 7:
repli :: Int -> [a] -> [a]
repli :: Int -> [a] -> [a]
repli n = foldr (\ x xs -> replicate n x ++ xs) []
repli n = concatMap (replicate n)

Revision as of 08:58, 13 December 2009

The prototype for repli in problem 15 is

repli :: [a] -> Int -> [a]

Because the second parameter is the number of times to replicate, it discourages the use function composition. I mean that if you swapped the parameters you could write it pointfree:

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

This would also match the way replicate is defined:

replicate :: Int -> a -> [a]

So, I suggest modifying problem 15 by swapping the parameters to repli in the example and the solution.

I made an edit to this page. I removed the following solution to problem 18:

slice xs i j = [xs!!(i-1)..xs!!(j-1)]


slice [1,3,6,3,1,6,7,8,3,2,4,76,8] 4 5 == []

Thanks to pixel for pointing this out.

The solution to problem 20 seems to be using 0-based indexing, whereas the question called for 1-based indexing in the other languages. This can be easily fixed:

removeAt :: Int -> [a] -> (a, [a])
removeAt k l = (elementAt l k, take (k-1) l ++ drop k l)

using elementAt from a previous problem.

or if you want to express that 1-based indexing is silly,

removeAt n+1 xs = (xs!!n,take n xs ++ drop (n+1) xs)