2
movex [] a s = []    
movex (x:xs) a s
| elem a x = moveNow x a s
| otherwise = x : (movex xs a s)
where
  moveNow x a s
    | s == 'l' = moveNow2 x a
    where
        moveNow2 [] _ = []
        moveNow2 (x:y:xs) a
          | x == ' ' && y == a = a : x : moveNow2 (y:xs) a
          | otherwise = x : moveNow2 (y:xs) a

<- This is what I got right now

I am trying to make a function that iterates through [string], finds the right string and then mutates it.

given input

func ["abc", "dfg"] f l -- move f in this list 1 space left --

expected output

["abc", "fdg"]

Right now I am stuck at movex function that gives me error

Couldn't match expected type `Char' with actual type `[Char]'
In the first argument of `(:)', namely `x'
In the expression: x : (movex xs a s)
3
  • | elem a x = moveNow x a s but what about xs? How can you drop it? Perhaps try giving your functions explicit type signatures. Commented Dec 23, 2015 at 13:28
  • what is the use of this condition x==' ' ? Commented Dec 23, 2015 at 15:01
  • use for condition x==' ' is to check whether x is a space character or not Commented Dec 26, 2015 at 22:23

1 Answer 1

1

Direct solution to the error is to replace the line

| elem a x = moveNow x a s

With

| elem a x = moveNow x a s : movex xs a s

Or, probably

| elem a x = moveNow x a s : xs

Depending on what you want to do after the first match: continue looking for certain character, or leave other strings untouched.

Your moveNow function has return type String, or [Char], while movex has [String], or [[Char]], that's why compiler complains.

To avoid such problems(or fix them easier) consider writing explicit type signatures, like so:

movex :: [String]->String->String->[String]
Sign up to request clarification or add additional context in comments.

1 Comment

Replacing with | elem a x = moveNow x a s : xs is what I was after. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.