[Haskell-beginners] A simple remove function
codingtales at gmail.com
Sun Feb 19 19:25:02 CET 2012
It won't compile because the type of (:) is (:) :: a -> [a] -> [a]. It
takes an element on its left side. take returns a list (take :: Int ->
[a] -> [a]).
Instead of using the (:) operator, use the (++) operator. It appends a
list to another.
So, your function should be: myremove x s = (take (s-1) x) ++ (drop (s+1) x)
But it won't do what you're expecting it to do because, drop 3
"doaltan" would remove doa" and give you "lo". It removes the first 3
elements. So, your drop (s+1) would remove (3+1), elements, i.e,
"doal". Change it to drop s.
So, your final function should be:
myremove x s = (take (s-1) x) ++ (drop s x)
And by the way, welcome to Haskell!
On Sun, Feb 19, 2012 at 11:30 PM, bahadýr altan <doaltan at yahoo.co.uk> wrote:
> Hello. I'm trying to write a code which deletes the wanted element in a
> list. Like this : myremove "doaltan" 3 = "doltan". I wrote the code below
> but it doesn't even compile. Could you help me with fixing the code? Thanks!
> myremove x s = (take (s-1) x):(drop (s+1) x)
> Beginners mailing list
> Beginners at haskell.org
More information about the Beginners