\documentclass[12pt]{article}
q
\title{Sequences}

\usepackage{listings}

\begin{document}

An implementation for sequences in Haskell.

\lstset{
keywordstyle = \bfseries,
keywords={module,data,deriving,where,Eq,Read,Show,infixr}
}

The head of module defines what is exported. 
\begin{lstlisting}{} % file: seqs.hs


module Sequences(
	Seq,
        Deque,
        ap2Prep,
        insert,
        delete,
        insperms,
        delperms, 
        initsegs,
        allsufs,
        allsegs)

\end{lstlisting}

The module defines.
\begin{lstlisting}{} % file: seqs.hs

where


data Seq a  = NIL | Cons a (Seq a)
	      deriving (Eq, Read, Show)


(<|) :: (Eq a) => a -> Seq a -> Seq a

infixr 9 <|

(<|) x  y = Cons x y

(|>) :: (Eq a) => Seq a -> a -> Seq a

infixr 9 |>

(|>) NIL  x = x <| NIL
(|>) (Cons a x) b = a <| (x |> b)



data Deque a =  Bot | Prep a (Deque a) | Ap (Deque a) a
	 deriving (Eq, Read, Show)

ap2Prep :: Deque a -> Deque a
ap2Prep Bot = Bot
ap2Prep (Prep a x) = (Prep a (ap2Prep x))
ap2Prep (Ap Bot a) = Prep a Bot
ap2Prep (Ap (Prep a x) b) = Prep a (ap2Prep (Ap x b))
ap2Prep (Ap (Ap x a) b) = ap2Prep (Ap (ap2Prep (Ap x a)) b)



insert x [] = [[x]]
insert x ys = [(take n ys)++x:(drop n ys) | n<-[0..length ys]]
 
insperms [] = [[]]
insperms [x] = [[x]]
insperms (x:xs) = concat [(insert x ys) | ys <- insperms xs]


delete xs = [(xs!!(n-1), (take (n-1) xs)++(drop n xs)) | n <-[1..length xs]]

delperms [] = [[]]
delperms xs = concat [map (y:) (delperms ys) | (y,ys) <- delete xs] 

initsegs xs = [(take (n+1) xs) | n<-[0..(length xs -1)]]

allsufs xs = [(drop n xs) | n<-[0..(length xs -1)]]

allsegs xs = concat [allsufs ys | ys <- initsegs xs]
 

\end{lstlisting}




\end{document}


 

