From 2b722cf3bdd267c999f25c4f1c2ebb4cc4d27cb4 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Mon, 2 Dec 2013 22:56:10 +0100 Subject: add other haskell files --- ch04_suffixtree.hs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 ch04_suffixtree.hs (limited to 'ch04_suffixtree.hs') diff --git a/ch04_suffixtree.hs b/ch04_suffixtree.hs new file mode 100644 index 0000000..a677ea4 --- /dev/null +++ b/ch04_suffixtree.hs @@ -0,0 +1,28 @@ +import Data.List (tails) + +suffixes :: [a] -> [[a]] +suffixes xs@(_:xs') = xs : suffixes xs' +-- as pattern. +-- binds xs to the value on the right of the @ +suffixes _ = [] + +-- same thing without as-pattern +-- This will do a copy of the data, the as-pattern doesn't + +suffixesNoAs (x:xs) = (x:xs) : suffixesNoAs xs +suffixesNoAs _ = [] + +-- even better +suffixes2 :: [a] -> [[a]] +suffixes2 xs = init (tails xs) + + +compose :: (b -> c) -> (a -> b) -> a -> c +compose f g x = f (g x) + +suffixes3 xs = compose init tails xs + +suffixes4 = compose init tails + +-- no need to invent the compose function +suffixes5 = init . tails -- cgit v1.2.3