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

From HaskellWiki
Jump to: navigation, search
(problem 20 correction?)
(suggest modifying problem 15)
Line 1: Line 1:
  +
The prototype for repli in problem 15 is
  +
<haskell>
  +
repli :: [a] -> Int -> [a]
  +
</haskell>
  +
  +
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:
  +
<haskell>
  +
repli :: Int -> [a] -> [a]
  +
repli n = foldr (\ x xs -> replicate n x ++ xs) []
  +
</haskell>
  +
  +
This would also match the way replicate is defined:
  +
<haskell>
  +
replicate :: Int -> a -> [a]
  +
</haskell>
  +
  +
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:
 
I made an edit to this page. I removed the following solution to problem 18:
   
Line 11: Line 31:
 
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:
 
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:

Revision as of 09:51, 24 April 2008

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 = foldr (\ x xs -> replicate n x ++ xs) []

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)