-3

How I can permute one symbol back. I have a given string: abcdpqrs, where output will be: badcqpsr.

My current code:

f s = foldr (\a ~(x,y) -> (a:y,x)) ([],[]) s

main :: IO ()
main = do 
    str <- getLine
    print(f str)

Evaluating f "abcdpqrs" results in

("acpr",
 "bdqs")

How can this be used to get "badcqpsr"?


Updated code:

rev (a : b : xs) = b : a : rev xs
rev xs = xs
main = do
    n <- getLine
    l <- getContents
    putStr (rev l)

When input are 2 strings, abcdpqrs and az result is badcqpsra and z. Expected output is badcqpsr and za. How I can fix it?

rel1x
  • 2,351
  • 4
  • 34
  • 62
  • 2
    What have you tried? Why didn't it work? Perhaps the answers to a related question will help you. http://stackoverflow.com/q/27307709/414413 – Cirdec Dec 13 '14 at 17:49
  • 1
    What would the output be if there were an odd number of elements in the list like in `"abcde"`? – Cirdec Dec 13 '14 at 17:54
  • @Cirdec because I don't now how to start! :( – rel1x Dec 13 '14 at 17:58
  • 4
    @pertpoert `0`? as an integer? That's not possible if the "normal" output has type `String`. *I don't now how to start!* You have to demonstrate that you made some effort towards finding a solution. Otherwise, your question will get closed pretty quickly. – jub0bs Dec 13 '14 at 17:58
  • @pertpoert You could try copying the code from the question I linked and try running it on `"abcdpqrs"`. What does that have to do with your problem? Then try changing the `(x, y) :` part of the third line to something else. What does it do if you put in `"1" : "2" :` instead? – Cirdec Dec 13 '14 at 18:04
  • @pertpoert From what you have now, you might be interested in Daniel Velkov's answer to a related question. http://stackoverflow.com/a/3987188/414413 However, you might get much more understanding from tinkering with Dato's `pack` from the first question I linked. http://stackoverflow.com/q/27307709/414413 – Cirdec Dec 13 '14 at 19:11
  • @Cirdec I don't understand how I can modify it's code and so I asked a question here. Nobody answered, only comments and helpful minus – rel1x Dec 14 '14 at 05:05
  • @Cirdec I do not know how to do it and so I asked for help – rel1x Dec 14 '14 at 05:06

1 Answers1

0

Here's a hint:

reverseFirst2 :: [a] -> [a]
reverseFirst2 (x1:x2:xs) = x2:x1:xs
reverseFirst2 xs = xs

This reverses the order of the first 2 elements of a list

> reverseFirst2 "abcdpqrs"
"bacdpqrs"

Can you fill in the ... below to do the same thing to the remainder of the list?

reverseEvery2 :: [a] -> [a]
reverseEvery2 (x1:x2:xs) = x2:x1: ...
reverseEvery2 xs = xs

Getting input

To get input one line at a time, read the input one line at a time.

main = do
    l <- getLine
    print (reverseEvery2 l)
    main

The reason you are getting that behavior with getContents is the input read from getContents includes the newline characters at the end of the lines. It is essentially running

rev "abcdpqrs\naz\n" =
    "badcqpsra\n\nz"
Cirdec
  • 24,019
  • 2
  • 50
  • 100
  • Thank you so much, but when input are 2 strings, `abcdpqrs` and `az` result is `badcqpsra` and `z`. Expected output is `badcqpsr` and `za` – rel1x Dec 16 '14 at 16:08
  • @pertpoert Please edit your question with what you have now. A very simple substitution for `...` in `reverseEvery2` results in the expected output for both of those inputs. – Cirdec Dec 16 '14 at 17:36
  • @pertpoert I'd recommend avoiding `getContents` and anything else that performs lazy IO until you understand it well enough to decide for yourself not to use lazy IO. – Cirdec Dec 17 '14 at 02:41
  • `n <- getLine` here is the number of test cases. I need to skip this is number in reverse function – rel1x Dec 17 '14 at 02:48