Data.Algorithm.Patience

patience-0.1.1: Patience diff and longest increasing subsequence

Safe Haskell | Safe
Language | Haskell98

Data.Algorithm.Patience

Contents

- Patience diff
- Longest increasing subsequence

Description

Implements "patience diff" and the patience algorithm for the longest increasing subsequence problem.

Synopsis

- diff :: Ord t => [t] -> [t] -> [Item t]
- data Item t
  - = Old t
  - | New t
  - | Both t t
- itemChar :: Item t -> Char
- itemValue :: Item t -> t
- longestIncreasing :: [(Int, a)] -> [(Int, a)]

# Patience diff

diff :: Ord t => [t] -> [t] -> [Item t] #

The difference between two lists, according to the "patience diff" algorithm.

data Item t #

An element of a computed difference.

Constructors

Old t | Value taken from the "old" list, i.e. left argument to `diff`
New t | Value taken from the "new" list, i.e. right argument to `diff`
Both t t | Value taken from both lists. Both values are provided, in case your type has a non-structural definition of equality.

Instances

Functor Item # |
Methods fmap :: (a -> b) -> Item a -> Item b # (<$) :: a -> Item b -> Item a #
Eq t => Eq (Item t) # |
Methods (==) :: Item t -> Item t -> Bool # (/=) :: Item t -> Item t -> Bool #
Data t => Data (Item t) # |
Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Item t -> c (Item t) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Item t) # toConstr :: Item t -> Constr # dataTypeOf :: Item t -> DataType # dataCast1 :: Typeable (* -> *) t => (forall d. Data d => c (t d)) -> Maybe (c (Item t)) # dataCast2 :: Typeable (* -> * -> *) t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Item t)) # gmapT :: (forall b. Data b => b -> b) -> Item t -> Item t # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Item t -> r # gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Item t -> r # gmapQ :: (forall d. Data d => d -> u) -> Item t -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Item t -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Item t -> m (Item t) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Item t -> m (Item t) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Item t -> m (Item t) #
Ord t => Ord (Item t) # |
Methods compare :: Item t -> Item t -> Ordering # (<) :: Item t -> Item t -> Bool # (<=) :: Item t -> Item t -> Bool # (>) :: Item t -> Item t -> Bool # (>=) :: Item t -> Item t -> Bool # max :: Item t -> Item t -> Item t # min :: Item t -> Item t -> Item t #
Read t => Read (Item t) # |
Methods readsPrec :: Int -> ReadS (Item t) # readList :: ReadS [Item t] # readPrec :: ReadPrec (Item t) # readListPrec :: ReadPrec [Item t] #
Show t => Show (Item t) # |
Methods showsPrec :: Int -> Item t -> ShowS # show :: Item t -> String # showList :: [Item t] -> ShowS #

itemChar :: Item t -> Char #

The character `'-'` or `'+'` or `' '` for `Old` or `New` or `Both` respectively.

itemValue :: Item t -> t #

The value from an `Item`. For `Both`, returns the "old" value.

# Longest increasing subsequence

longestIncreasing :: [(Int, a)] -> [(Int, a)] #

Given: a list of distinct integers. Picks a subset of the integers in the same order, i.e. a subsequence, with the property that

- it is monotonically increasing, and
- it is at least as long as any other such subsequence.

This function uses patience sort: http://en.wikipedia.org/wiki/Patience_sorting. For implementation reasons, the actual list returned is the reverse of the subsequence.

You can pair each integer with an arbitrary annotation, which will be carried through the algorithm.