Created
May 21, 2014 12:39
-
-
Save jdnavarro/d8882282405e4e20a025 to your computer and use it in GitHub Desktop.
Cleaned up code for http://stackoverflow.com/a/23768724/482696
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE LambdaCase #-} | |
module Main where | |
import Control.Category | |
import Data.Traversable | |
import Data.Tree | |
import Control.Comonad | |
import Data.List.NonEmpty | |
trav :: Eq a => (Traversable t, Comonad t) => [a] -> t a -> [a] | |
trav [] _ = [] | |
trav p@(s0:ss0) w | |
| extract w /= s0 = p | |
| otherwise = case traverse (step ss0) (duplicate w) of | |
Left ss1 -> ss1 | |
Right _ -> p | |
where | |
step [] = const $ Right () | |
step (s:ss) = trav ss >>> \case | |
(s':_) | s' == s -> Right () | |
ss2 -> Left ss2 | |
path = ['a', 'b', 'c', 'd', 'e'] | |
tree = Node 'a' [Node 'x' [], Node 'c' [Node 'z' [Node 'd' []]]] | |
list = 'a' :| ['b', 'c', 'd', 'x', 'e'] | |
main :: IO () | |
main = do print $ trav path tree | |
print $ trav path list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment