How to define a rotates function

后端 未结 8 1950
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-11 14:47

How to define a rotates function that generates all rotations of the given list?

For example: rotates [1,2,3,4] =[[1,2,3,4],[2,3,4,1],[3,4,1,2],[4,1,2,3]]

相关标签:
8条回答
  • 2021-01-11 15:31

    I suggest:

    rotate l = l : rotate (drop 1 l ++ take 1 l)
    
    distinctRotations l = take (length l) (rotate l)
    
    0 讨论(0)
  • 2021-01-11 15:33
    myRotate lst = lst : myRotateiter lst lst
      where myRotateiter (x:xs) orig
              |temp == orig = []
              |otherwise = temp : myRotateiter temp  orig               
              where temp = xs ++ [x]
    
    0 讨论(0)
提交回复
热议问题