functional programming - Type error in explicitly typed binding (Haskell) -
i writing simple function transforms string replacing combinations of characters another. (e.g. "ab" "a") noob in haskell, , error: "type error in explicitly typed binding"
could please tell me problem? code:
transform :: string -> string transform [] = [] transform [x] = x transform (x:y:xs) | x == "a" && y == "b" = "a" ++ (transform xs) | x == "b" && y == "a" = "b" ++ (transform xs) | x == "b" && y == "b" = "a" ++ (transform xs) | x == "a" && y == "a" = "aaa" ++ (transform xs)
thank you!
in final transform
pattern, using string literals "a" , "b" when should characters.
transform :: string -> string transform [] = [] transform [x] = [x] transform (x:y:xs) | x == 'a' && y == 'b' = "a" ++ (transform xs) | x == 'b' && y == 'a' = "b" ++ (transform xs) | x == 'b' && y == 'b' = "a" ++ (transform xs) | x == 'a' && y == 'a' = "aaa" ++ (transform xs)
also, there bug in second transform definition, needed wrap x
in brackets because returning list of characters.
(you may want have final pattern match handle other input, since you'll non-exhaustive error if run against string "zzz")
Comments
Post a Comment