Difference between revisions of "Talk:99 questions/11 to 20"
Jump to navigation
Jump to search
ScottWolchok (talk | contribs) (problem 20 correction?) |
|||
Line 10: | Line 10: | ||
Thanks to pixel for pointing this out.</haskell> |
Thanks to pixel for pointing this out.</haskell> |
||
+ | |||
+ | |||
+ | 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: |
||
+ | |||
+ | <haskell> |
||
+ | removeAt :: Int -> [a] -> (a, [a]) |
||
+ | removeAt k l = (elementAt l k, take (k-1) l ++ drop k l)</haskell> |
||
+ | using elementAt from a previous problem. |
||
+ | |||
+ | |||
+ | or if you want to express that 1-based indexing is silly, |
||
+ | <haskell> |
||
+ | removeAt n+1 xs = (xs!!n,take n xs ++ drop (n+1) xs)</haskell> |
Revision as of 03:51, 28 December 2007
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)