-1

I have a given string: abcdpqrs, where output will be: badcqpsr.

My current code:

f :: [a] -> [a]
f (a:b:xs) = b:a:xs
f xs = xs

Evaluating f "abcdpqrs" results in "bacdpqrs". How can this be used to get "badcqpsr"?

rel1x
  • 2,351
  • 4
  • 34
  • 62
  • 4
    You asked the same question a couple of days ago and it got closed. Please do not post the same question multiple times... Instead, you should edit your original question and add your current attempt there. Anyway, you're missing a recursive call: it should be `f (a:b:xs) = b : a : f xs`. – jub0bs Dec 16 '14 at 11:38
  • @Jubobs thank you, if you change your comment to answer, I will select it. – rel1x Dec 16 '14 at 11:43

1 Answers1

2

Try processing more than just the first two characters by recursing on the remainder of the list:

f :: [a] -> [a]
f (a:b:xs) = b:a:f xs
f xs = xs
Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207