Difference between revisions of "99 questions/Solutions/82"

From HaskellWiki
Jump to: navigation, search
(Solution to Problem 82 using list comprehension added)
 
(categorize)
Line 10: Line 10:
 
                 arrive = fst split
 
                 arrive = fst split
 
                 go ls = [ x ++ [snd y] | x <- ls, y <- g, last x == fst y, not (snd y `elem` tail x)]
 
                 go ls = [ x ++ [snd y] | x <- ls, y <- g, last x == fst y, not (snd y `elem` tail x)]
 +
 +
[[Category:Programming exercise spoilers]]

Revision as of 03:49, 10 January 2017

Brute-force search from the source, using list comprehension:

import Data.List (partition)
cycle' :: Int -> [(Int, Int)] -> [ [Int] ]
cycle' n g = search n []
  where search [] result = result
        search cur result = search (go active) (arrive ++ result)
          where split = partition end cur
                end s = (last s == n) && (length s /= 1)
                active = snd split
                arrive = fst split
                go ls = [ x ++ [snd y] | x <- ls, y <- g, last x == fst y, not (snd y `elem` tail x)]