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

From HaskellWiki
Jump to navigation Jump to search
m
Line 44: Line 44:
 
<haskell>
 
<haskell>
 
removeAt n+1 xs = (xs!!n,take n xs ++ drop (n+1) xs)</haskell>
 
removeAt n+1 xs = (xs!!n,take n xs ++ drop (n+1) xs)</haskell>
  +
  +
----
  +
  +
I fixed problem with inconsistent indexing between examples for problem 20.
  +
I think something need to be done for one of the solutions for problem 19:
  +
<haskell>
  +
rotate xs n = take (length xs) $ drop (length xs + n) $ cycle xs</haskell>
  +
It works incorrectly for negative n < length xs.
  +
But it's example how to implement rotate "without mod" and I have no idea how to easily fix it to keep this property. Maybe it should be changes to use mod or removed at all?

Revision as of 20:10, 8 April 2013

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

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)

I fixed problem with inconsistent indexing between examples for problem 20. I think something need to be done for one of the solutions for problem 19:

rotate xs n = take (length xs) $ drop (length xs + n) $ cycle xs

It works incorrectly for negative n < length xs. But it's example how to implement rotate "without mod" and I have no idea how to easily fix it to keep this property. Maybe it should be changes to use mod or removed at all?