Home > Net >  How to combine two lists created with show in Haskell?
How to combine two lists created with show in Haskell?

Time:03-29

I've been trying to solve a practice problem in which I must create a function which rotates a specific digit from the left to the right as shown here:

print $ digRotator 68957 2 == 68579
print $ digRotator 68579 3 == 68597

The first argument is the number and the second - the saved digit before the target one. In the first one it's 2 so 6 and 8 are saved and 9 is rotated. So far I've tried this:

rotate :: [a] -> [a]
rotate [] = []
rotate (a:as) = as    [a]

digRotator :: Int -> Int -> Int
digRotator n keeper
  | keeper == 0 = (read . rotate . show) n
  | otherwise = read ((rotate (drop keeper (show n)))    (take keeper (show n)))

It gives me error on the last read function.

Parse error: module header, import declaration
    or top-level declaration expected.
  |
1 | (rotate (drop keeper (show n)))    (take keeper (show n))
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Thanks in advance!

CodePudding user response:

You have accidentally copied and pasted code from the end of what you posted here to the beginning of your file. Scroll up and delete it and you should be good to go. You will have a bug or two to fix, but from what I see here you will be more than capable of it.

  • Related