[Haskell] Function to replace given element in list

Niely Boyken niely.b0yk3n at gmail.com
Tue Jul 19 20:08:29 UTC 2016


Hi

I'm trying to make a custom function to replace a given element in a list.

Code:
    let i = elemIndex toReplace lst in

        case i of
            Just i ->
                let z = splitAt i lst
                    x = fst z
                    y = (snd z)
                    in
                        init x
                        x ++ newNmr
                        x ++ y

            Nothing -> [5]

Error:

> 1)
>  * Couldn't match expected type `[a] -> [a]' with actual type `[a]'
>     * The function `init' is applied to two arguments,
>       but its type `[a] -> [a]' has only one
>       In the first argument of `(++)', namely `init x x'
>       In the expression: init x x ++ newNmr x ++ y
>     * Relevant bindings include
>         y :: [a] (bound at C:\users\niel\desktop\test2.hs:41:21)
>         x :: [a] (bound at C:\users\niel\desktop\test2.hs:40:21)
>         z :: ([a], [a]) (bound at C:\users\niel\desktop\test2.hs:39:21)
>         newNmr :: [a] (bound at C:\users\niel\desktop\test2.hs:34:30)
>         lst :: [a] (bound at C:\users\niel\desktop\test2.hs:34:26)
>         toReplace :: a (bound at C:\users\niel\desktop\test2.hs:34:16)
>         (Some bindings suppressed; use -fmax-relevant-binds=N or
> -fno-max-relevant-binds)
> 2)
>  * Couldn't match expected type `[a] -> [a]' with actual type `[a]'
>     * The function `newNmr' is applied to one argument,
>       but its type `[a]' has none
>       In the first argument of `(++)', namely `newNmr x'
>       In the second argument of `(++)', namely `newNmr x ++ y'
>     * Relevant bindings include
>         y :: [a] (bound at C:\users\niel\desktop\test2.hs:41:21)
>         x :: [a] (bound at C:\users\niel\desktop\test2.hs:40:21)
>         z :: ([a], [a]) (bound at C:\users\niel\desktop\test2.hs:39:21)
>         newNmr :: [a] (bound at C:\users\niel\desktop\test2.hs:34:30)
>         lst :: [a] (bound at C:\users\niel\desktop\test2.hs:34:26)
>         toReplace :: a (bound at C:\users\niel\desktop\test2.hs:34:16)
>         (Some bindings suppressed; use -fmax-relevant-binds=N or
> -fno-max-relevant-binds)
>

I've tried a lot, but I always got an error.
What am I doing wrong?

Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell/attachments/20160719/145f877f/attachment.html>


More information about the Haskell mailing list