summaryrefslogtreecommitdiffstats
path: root/ch04_suffixtree.hs
blob: a677ea44556c11ee4033c327f20b990524c04ca9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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