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.0.0.5 tracks/haskell/exercises/dominoes/examples/success-standard/src/Dominoes.hs