Just learn the haskell by myself, and can not solve this problem. These are the codes.
subst :: Eq t=>t->t->[t]->[t]
subst a b [xs]=[if x==a then b else x | x <- xs]
The error is below.
subst.hs:2:46:
Could not deduce (t ~ [t])
from the context (Eq t)
bound by the type signature for
subst :: Eq t => t -> t -> [t] -> [t]
at subst.hs:1:10-29
‘t’ is a rigid type variable bound by
the type signature for subst :: Eq t => t -> t -> [t] -> [t]
at subst.hs:1:10
Relevant bindings include
xs :: t (bound at subst.hs:2:12)
b :: t (bound at subst.hs:2:9)
a :: t (bound at subst.hs:2:7)
subst :: t -> t -> [t] -> [t] (bound at subst.hs:2:1)
In the expression: xs
In a stmt of a list comprehension: x <- xs
The problem, I guess, is that haskell can not ensure the element from [t] match the t. I am not sure. And I want to know how to solve this problem.
you just have one [..] wrap to many:
subst :: Eq t=>t->t->[t]->[t]
subst a b xs = [if x==a then b else x | x <- xs]
it's just xs not [xs]
The reason is simple: if you write [xs] you tell Haskell to expect a list with an single entry xs and it will try to pattern-match it - after this you tell it to pull out values x from xs (x <- xs) that tells Haskell that xs has to be some kind of list itself. so in the end it will assume t to be some list t ~ [s]. But then you go on and check x == a and here x :: s and a :: t ending with the error as now [s] ~ t ~ s.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With