Fork me on GitHub

Print String的问题




--  define a Point as a pair of integers
type Point = (Int,Int)

--  define the point we call the origin 
origin :: Point
origin = (0,0)

-- define a function for scaling a point
-- horizontally by h and vertically by v
scale :: Int -> Int -> Point -> Point
scale h v (x,y) = (h*x,v*y)

-- flip a point about the  horizontal axis (y=0)
flipH :: Point -> Point
flipH (x,y) = (x,-y)

-- flip a point about the  vertical axis (x=0)
flipV :: Point -> Point
flipV (x,y) = (-x,y)
-- flip a point about the diagonal line (x=y)
flipD :: Point -> Point
flipD (x,y) = (y,x)

-- quarter turn clockwise
turnC :: Point -> Point
turnC (x,y) = (y,-x)

-- half turn 
turnB :: Point -> Point
turnB (x,y) = (-x,-y)
-- quarter turn anticlockwise
turnA :: Point -> Point
turnA (x,y) = (-y,x)


-- define an Image as a list of points
type Image = [Point]

-- define a T-shaped image
t :: Image

-- transform an Image point by point
pointwise :: (Point->Point) -> (Image->Image)
pointwise f = \ps -> [f p | p <- ps]

-- overlay two images
overlay :: Image -> Image -> Image
overlay i j = i ++ j


shift :: Int -> Int -> Point -> Point
shift h v (x, y) = (x+h, y+v)


blur :: Image -> Image
blur i =      let j = pointwise(flipV.turnC) i in overlay i j


x :: Image
x = [(1,1), (1,-1), (2,0), (3,1), (3,-1)]


head :: [a] -> a
head (x:_) = x


maxx :: Image -> Int
maxx [(x,y)] = x
maxx ((x,y):t) = max x (maxx t)


maxy :: Image -> Int
maxy [(x,y)] = y
maxy ((x,y):t) = max y (maxy t)


minx :: Image -> Int
minx [(x,y)] = x
minx ((x,y):t) = min x (minx t)


miny :: Image -> Int
miny [(x,y)] = y
miny ((x,y):t) = min y (miny t)


bounds :: Image -> (Int,Int,Int,Int)
bounds x = (minx x, maxx x, miny x, maxy x)


sideBySide :: Image -> Image -> Image
sideBySide x t =  (pointwise (shift (-minx x) (-miny x)) x) ++ (pointwise (shift (-maxx t) (-maxy t)) t)


spilt :: Int -> String -> String

spilt n s 
    | s == ""    = ""

    | otherwise = fst(splitAt n s) ++ "\n" ++ spilt n (snd(splitAt n s)) 


render :: Image -> String 
render t = 



Define an image x consisting of 5 points (1,1), (1,-1), (2,0), (3,1) and (3,-1). Recall that, diagramatically, x can be represented as follows (where the points in the image are denoted 'x', the origin is denoted '+', and the horizontal and vertical axes are denoted '-' and '|' respectively):


Add a function bounds which maps a given image to its bounding-box (xmin,xmax,ymin,ymax). As an example, bounds x should return
(Note: you may use the built-in functions min x y and max x y which return the minimum and maximum of two integers x and y, respectively)
Up to 20%

Add a function sideBySide that takes two images and returns the overlaid image obtained after shifting them so as to ensure their lowest points sit on the x-axis and their rightmost/leftmost points touch the y-axis, respectively. As an example, sideBySide x (pointwise (scale 2 3) t) should return the image represented by the following diagram:


Up to 40%

Add a function split which takes an integer n and a string s and returns the string obtained by inserting a newline character '' into s after every n characters. You may assume the length of s is a multiple of n. As an example, split 3 "Hello World!" should return
(Note: you may use the built-in function splitAt n s which splits a string s into two substrings (a,b) where a is the first n characters and b is the rest.
Up to 60%

Add a function render which takes an image and returns a string that, if printed, would give the diagrammatic representation of the image as illustrated above (making sure to include the one point border around all sides of the image). As an example, render t should return


which prints to the following (as can be shown using the command putStr):


Up to 100%

Submitted by at 4 years ago