Sha256: 8ea703833850a4afcc49b927640aae26c8cdd7ab395227a528d47a35fffc91bc

Contents?: true

Size: 703 Bytes

Versions: 381

Compression:

Stored size: 703 Bytes

Contents

module Dominoes (chain) where

import Data.List  (delete)
import Data.Maybe (listToMaybe, mapMaybe)

chain :: [(Int, Int)] -> Maybe [(Int, Int)]
chain [] = Just []
chain (x@(left, right):xs) = fmap (x:) (subchain xs left right)

subchain :: [(Int, Int)] -> Int -> Int -> Maybe [(Int, Int)]
subchain [] chainLeft chainRight = if chainLeft == chainRight then Just [] else Nothing
subchain l  chainLeft chainRight = listToMaybe (mapMaybe subchain' l)
  where subchain' (a, b) | a == chainRight = fmap ((a, b):) (subchain (delete (a, b) l) chainLeft b)
        subchain' (a, b) | b == chainRight = fmap ((b, a):) (subchain (delete (a, b) l) chainLeft a)
        subchain' _                        = Nothing

Version data entries

381 entries across 381 versions & 1 rubygems

Version Path
trackler-2.2.1.98 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs
trackler-2.2.1.97 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs
trackler-2.2.1.96 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs
trackler-2.2.1.95 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs
trackler-2.2.1.94 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs
trackler-2.2.1.93 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs
trackler-2.2.1.92 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs
trackler-2.2.1.91 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs
trackler-2.2.1.90 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs
trackler-2.2.1.89 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs
trackler-2.2.1.88 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs
trackler-2.2.1.87 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs
trackler-2.2.1.86 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs
trackler-2.2.1.85 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs
trackler-2.2.1.84 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs
trackler-2.2.1.83 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs
trackler-2.2.1.82 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs
trackler-2.2.1.81 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs
trackler-2.2.1.80 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs
trackler-2.2.1.79 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs