
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)

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
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Warm Regards, AbdulSattar Mohammed

Am 19.02.2012 19:00, schrieb bahadýr altan:
myremove x s = (take (s-1) x):(drop (s+1) x)
take :: Int -> [a] -> [a] drop:: Int -> [a] -> [a] Both functions return a list, therefore you need the "++" operator to append two lists: (++): [a] -> [a] -> [a] So this is what you get: (I changed s and x becausee I associate s with a string and x with a number) myremove :: String -> Int -> String myremove s x = (take (x-1) s) ++ (drop (x) s) Notice that you have to drop x elements (and not x+1). http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.htm...

Look at the types of (:), take, and drop. You want to concat lists, not append an element to the head of a list. I would recommend looking at the concat operator (++). Haskell is all about the types! take returns a list, as does drop. So you need some function of type [a] -> [a] -> [a], not a -> [a] -> [a].
participants (4)
-
AbdulSattar Mohammed
-
bahadýr altan
-
Benjamin Edwards
-
Tobias