-- CSci 555, Functional Programming, Spring 2007
-- Assignment #2

-- H. Conrad Cunningham

--  BEGIN function defintions from section 7.6 of
--  S. Thompson. _Haskell: The Craft of Functional Progamming_ 
--  Second Edition, Addison Wesley, 1999.

-- Add an import of the Char module to allow functions like isAlphaNum to
-- work

import Char

-- Type synonyms for words and lines
type Word = String
type Line = [Word]

--The "whitespace" characters.
whitespace :: String
whitespace = ['\n','\t',' ']

-- Get a word from the front of a string.
getWord :: String -> String
getWord []                = [] 
getWord (x:xs) 
    | elem x whitespace   = []
    | otherwise           = x : getWord xs
    
-- Drop the first word of a string.
dropWord :: String -> String
dropWord []               = []
dropWord (x:xs) 
    | elem x whitespace   = (x:xs)
    | otherwise           = dropWord xs

-- Remove the whitespace character(s) from the front of a string.
dropSpace :: String -> String
dropSpace []              = []
dropSpace (x:xs) 
    | elem x whitespace   = dropSpace xs
    | otherwise           = (x:xs)

-- Split a string into words.
splitWords :: String -> [Word]
splitWords st = split (dropSpace st)

split :: String -> [Word]
split [] = []
split st = (getWord st) : split (dropSpace (dropWord st))

-- Split into lines of length at most lineLen.
lineLen :: Int
-- lineLen = 80
lineLen = 10	-- for testing purposes

-- Get a line from a list 
getLine2 :: Int -> [Word] -> Line
getLine2 len []           = []
getLine2 len (w:ws)
    | length w <= len     = w : restOfLine  
    | otherwise           = []
    where
        newlen      = len - (length w + 1)
        restOfLine  = getLine2 newlen ws

-- Drop the first line from a list of words.
-- dropLine :: Int -> [Word] -> Line
-- dropLine = dropLine 	-- DUMMY DEFINITION

-- Split into lines.
splitLines :: [Word] -> [Line]
splitLines [] = []
splitLines ws = getLine2 lineLen ws
                    : splitLines (dropLine lineLen ws)

-- Fill a text string into lines.
fill :: String -> [Line]
fill = splitLines . splitWords

-- END function definitions from Thompson textbook
