Created
July 7, 2014 11:32
-
-
Save blouerat/98345e52ad6bfa8c8fa9 to your computer and use it in GitHub Desktop.
1HaskellADay: roll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Tuple | |
{-- | |
Okay, shoot! Fell asleep while composing a problem; sorry for the delay! | |
So, this one is an easy one. We get a little Forth'y'. This is from P19 | |
of the P99 problem-set. | |
--} | |
roll :: [a] -> Int -> [a] | |
roll [] _ = [] | |
roll list n = uncurry (++) . swap . splitAt i $ list | |
where i = mod n $ length list | |
{-- | |
roll rotates list n steps to the left. | |
roll [1,2,3] 1 ~> [2,3,1] | |
The argument n can be negative, indicating that the rotation is to occur | |
to the right, then, instead. | |
roll [1,2,3] (-1) ~> [3,1,2] | |
(Recall that we write '(-1)' and not just '-1' because '-1' is interpreted | |
as the binary operator (-) applied to the Int 1) | |
The list can be rotated multiple times, so: | |
roll [1,2,3] 9 ~> [1,2,3] | |
Have at it! | |
Explanation: the term ROLL comes from the FORTH programming language, | |
placing the n-th element of the stack on TOS ('top of stack'). | |
--} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment