fromWord p = recurse 1 [] (Set.fromList [1..length p]) where
  recurse x currentCycle unseen
    | null unseen       = [reverse currentCycle]
    | x `Set.member` unseen = recurse (p !! (x-1)) (x:currentCycle) (Set.delete x unseen)
    | otherwise         = reverse currentCycle : recurse (minimum unseen) [] unseen
fromWord p = recurse 1 [] (Set.fromList [1..length p]) where
  recurse x currentCycle unseen
    | null unseen       = [reverse currentCycle]
    | x `Set.member` unseen = recurse (p !! (x-1)) (x:currentCycle) (Set.delete x unseen)
    | otherwise         = reverse currentCycle : recurse (minimum unseen) [] unseen
fromWord p = recurse 1 [] (Set.fromList [1..length p]) where
  recurse x currentCycle unseen
    | null unseen       = [reverse currentCycle]
    | x `Set.member` unseen = recurse (p !! (x-1)) (x:currentCycle) (Set.delete x unseen)
    | otherwise         = reverse currentCycle : recurse (minimum unseen) [] unseen