[Haskell] Function to replace given element in list
Richard Fung
minesasecret at gmail.com
Tue Jul 19 20:23:20 UTC 2016
Hi Niely,
everything after the "in" in your "let in" is one expression. In other
words all of this:
init x
x ++ newNmr
x ++ y
is being read as one line.. so
init x x ++ newNmr x ++ y
which is why the compiler is complaining that you are applying init to two
values (init x x).
I think what you want is actually something more like
(init x) ++ [newNmr] ++ y
assuming newNmr is a number and not already a list.
I'm not entirely sure what you were trying to do there but you seem a bit
confused.. "init x" on its own doesn't actually do anything; it computes a
value but you aren't assigning it to anything here.
If you wanted to keep those exact expressions you could instead do
let x0 = init x
x1 = x0 ++ newNmr
in x1 ++ y
and x1 ++ y would be the thing that actually gets return.
In any case I am certainly no expert so hopefully someone else can explain
this better than I can.
On Tue, Jul 19, 2016 at 1:08 PM, Niely Boyken <niely.b0yk3n at gmail.com>
wrote:
> 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!
>
> _______________________________________________
> Haskell mailing list
> Haskell at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell/attachments/20160719/e935f70e/attachment.html>
More information about the Haskell
mailing list