Talk:99 questions/11 to 20

From HaskellWiki
Revision as of 03:51, 28 December 2007 by ScottWolchok (talk | contribs) (problem 20 correction?)

Jump to: navigation, search

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

Counter-example:

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)