Hello<br><br>While learning Haskell I&#39;m trying to solve some simple problems on <a href="http://spoj.pl">spoj.pl</a> occasionally. Currently I&#39;m working on: <a href="http://www.spoj.pl/problems/BWHEELER/">http://www.spoj.pl/problems/BWHEELER/</a>. I figured out how to solve it but I have some problems with reading input (that&#39;s my guess)<br>
<br>Here is my solution:<br><br>import Data.List<br>import Data.Array<br>import qualified Data.ByteString.Lazy.Char8 as BS<br>import IO<br><br>traverse :: Array Int (Char, Int) -&gt; Int -&gt; Int -&gt; String -&gt; String<br>
traverse endings n k acc = <br>    let (c,i) = endings ! n<br>    in if k == 0<br>        then acc<br>        else traverse endings i (k-1) (c:acc)<br><br>solve :: (Int, String) -&gt; String<br>solve (n,w) = <br>    let l = length w<br>
        endings = sort $ zip w [0..]<br>        endingsArray = array (0, l) (zip [0..] endings) <br>    in reverse $ traverse endingsArray (n-1) l &quot;&quot;<br><br>parseCases :: [BS.ByteString] -&gt; [(Int, String)]<br>
parseCases (l:l&#39;:ls) = <br>    let n = readInt l<br>        w = BS.unpack l&#39;<br>    in (n,w):parseCases ls<br>parseCases _ = []<br><br>main :: IO ()<br>main = do<br>  ls &lt;- BS.lines `fmap` (BS.readFile &quot;input.txt&quot;)              --BS.getContents<br>
  putStr $ unlines $ map solve $ parseCases ls<br><br>readInt :: BS.ByteString -&gt; Int<br>readInt x =<br>  case BS.readInt x of Just (i,_) -&gt; i<br>                       Nothing    -&gt; error (&quot;Unparsable Int&quot; ++ (show x)) <br>
<br><br>The input.txt file contains following text:<br>2<br>bacab<br>3<br>rwlb<br>11<br>baaabaaaabbbaba<br>0<br><br>When I compile and execute this code i get follwing output:<br>aaaaaa<br>lllll<br>bbb<br><br>It&#39;s different  when compared to this in ghci ( this is what I expect):<br>
 &gt; map solve [(2,&quot;bacab&quot;), (3, &quot;rwlb&quot;), (11,&quot;baaabaaaabbbaba&quot;)]<br>[&quot;abcba&quot;,&quot;rbwl&quot;,&quot;baaabbbbaaaaaab&quot;]<br><br>Can you explain me what I&#39;m doing wrong?<br>I appreciate any tips how to improve this code also.<br>
<br>Thanks for help!<br>Artur Tadra³a<br>