module CSE230.Doc
— * A document type
— * Constructors
Copyright By PowCoder代写 加微信 powcoder
, empty, doc
— * Accessors
, width, height
— * Example Doc
, aDoc, bDoc, lineDoc, animals, triangles
— * Combinators
, vcatL, vcatR, hcatB, hcatT
— * Properties
, prop_hcatT_width
, prop_hcatT_height
, prop_hcatB_width
, prop_hcatB_height
, prop_vcatL_width
, prop_vcatR_width
, prop_vcatL_height
, prop_vcatR_height
import qualified Test.QuickCheck as QC
import Prelude hiding (maximum)
import CSE230.List
——————————————————————————-
— | A ‘Doc’ is a ‘String’ list
——————————————————————————-
data Doc = D { docLines :: [String] }
deriving (Eq, Ord)
empty :: Doc
empty = D []
doc :: String -> Doc
doc s = D (lines s)
aDoc :: Doc
aDoc = D [ “a”
, “aaaaa”]
bDoc :: Doc
bDoc = D [ “b”
lineDoc :: Doc
lineDoc = doc “<----- HERE"
animals :: [Doc]
animals = [ doc "cat"
, doc "horse"
, doc "mongoose"
-------------------------------------------------------------------------------
-- | Printing a Doc
-------------------------------------------------------------------------------
-- >>> aDoc
—
instance Show Doc where
show (D ls) = unlines ls
——————————————————————————-
— | Generating Random Docs
——————————————————————————-
instance QC.Arbitrary Doc where
arbitrary = fmap D QC.arbitrary
——————————————————————————-
— | Dimensions of a ‘Doc’
——————————————————————————-
— >>> width aDoc
width :: Doc -> Int
width (D ls) = maximum 0 (map length ls)
— >>> height aDoc
height :: Doc -> Int
height (D ls) = length ls
——————————————————————————-
— | Vertical Concatenation aligned at Left
——————————————————————————-
— >>> (doc “cat”) `vcatL` (doc “horse”) `vcatL` (doc “mongoose”)
— mongoose
—
vcatL :: Doc -> Doc -> Doc
vcatL d1 d2 = error “fill this in”
——————————————————————————-
— | Vertical Concatenation aligned at Right
——————————————————————————-
— >>> (doc “cat”) `vcatR` (doc “horse”) `vcatR` (doc “mongoose”)
— cat
— horse
— mongoose
—
vcatR :: Doc -> Doc -> Doc
vcatR d1 d2 = error “fill this in”
——————————————————————————-
— | Horizontal Concatenation aligned at Top
— HINT: use `zip` or `zipWith`
——————————————————————————-
— >>> hcatT aDoc lineDoc
— a <----- HERE
--
— >>> hcatT aDoc bDoc
— aaa bbb
—
hcatT :: Doc -> Doc -> Doc
hcatT d1 d2 = error “fill this in”
elongate :: Dir -> Int -> Doc -> Doc
elongate dir h (D ls) = D (pad dir h “” ls)
——————————————————————————-
— | Horizontal Concatenation aligned at Bottom
— HINT: use `zip` or `zipWith`
——————————————————————————-
— >>> hcatB aDoc lineDoc
— aaaaa<----- HERE
--
— >>> hcatB aDoc bDoc
— aaaaabbb
—
hcatB :: Doc -> Doc -> Doc
hcatB d1 d2 = error “fill this in”
triangle :: Doc
triangle = D
, “*****” ]
— >>> foldr vcatL empty triangles
— *** ***
— **********
— * * *
— *** *** ***
— ***************
—
— >>> foldr vcatR empty triangles
— *
— ***
— *****
— * *
— *** ***
— **********
— * * *
— *** *** ***
— ***************
—
triangles :: [Doc]
triangles = [ triangle
, triangle `hcatT` triangle
, triangle `hcatT` triangle `hcatT` triangle ]
——————————————————————————-
— | Properties of `Doc` combinators ——————————————
——————————————————————————-
prop_hcatT_width d1 d2 = width (d1 `hcatT` d2) == width d1 + width d2
prop_hcatT_height d1 d2 = height (d1 `hcatT` d2) == max (height d1) (height d2)
prop_hcatB_width d1 d2 = width (d1 `hcatB` d2) == width d1 + width d2
prop_hcatB_height d1 d2 = height (d1 `hcatB` d2) == max (height d1) (height d2)
prop_vcatL_width d1 d2 = width (d1 `vcatL` d2) == max (width d1) (width d2)
prop_vcatR_width d1 d2 = width (d1 `vcatR` d2) == max (width d1) (width d2)
prop_vcatL_height d1 d2 = height (d1 `vcatL` d2) == height d1 + height d2
prop_vcatR_height d1 d2 = height (d1 `vcatR` d2) == height d1 + height d2
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com