--Exercise 6.35
--Exercise 6.34
--Exercise 6.33
--Exercise 6.32
printImage :: Image -> IO()
printImage i = print i
--Exercise 6.31
moveImage:: Image -> Int -> Int -> Image
moveImage (i,(x,y)) dx dy = (i,(x+dx,y+dy))
--Exercise 6.30
changePosition :: Image-> Position -> Image
changePosition (i,p) p1 = (i,p1)
--Exercise 6.29
makeImage :: String -> Position -> Image
makeImage s p = (s,p)
type Position = (Int,Int)
type Image = (String,Position)
--Exercise 6.28
--Exercise 6.27
--Exercise 6.26
--Exercise 6.25
--Exercise 6.24
zipCompact::Eq a =>[(a,Int)]->[(a,Int)]->[(a,Int)]
zipCompact a b
| (length a) == 0 = b
| otherwise = zipCompact (tail a) (addzip (head a) b)
addzip ::Eq a=> (a,Int)->[(a,Int)]->[(a,Int)]
addzip x [] = [x]
addzip (ax,ay) ((bx,by):xs)
| ax /= bx = (bx,by) : (addzip (ax,ay) xs)
| otherwise = (ax, ay+by) : xs
reverseCompact ::[(a,b)] -> [(b,a)]
reverseCompact xs = [ (y,x)|(x,y)<-xs]
--Exercise 6.23
--Exercise 6.22
--Exercise 6.21
--Exercise 6.20
--Exercise 6.19
--Exercise 6.18
--Exercise 6.17
myReplicate :: Int -> a -> [a]
myReplicate n x
| n ==1 = [x]
| otherwise = x : (myReplicate (n-1) x)
--Exercise 6.16
--Exercise 6.15
--Exercise 6.14
--Exercise 6.13
--Exercise 6.12
--Exercise 6.11
--Exercise 6.10
--Exercise 6.9
type Picture = [String]
--Exercise 6.8
rotate90 :: Picture -> Picture
rotate90 (x:xs)
| x == [] = [[]]
| xs==[[]] = [(leftCube (x:xs))]
| otherwise = (leftCube (x:xs)) :(rotate90 (rightCube (x:xs)))
rightCube :: Picture -> Picture
rightCube ((x1:x1s):xs)
| xs == [] = x1s : []
| otherwise = x1s : (rightCube xs)
leftCube :: Picture -> String
leftCube (x:xs)
| xs ==[] = (head x) : []
| otherwise = (head x) : (leftCube xs)
--Exercise 6.7
printPicture :: Picture -> IO()
printPicture (p:ps)
| ps== [] = print p
| otherwise = do
print p
printPicture ps
--Exercise 6.6
--superimpose :: Picture -> Picture -> Picture
--Exercise 6.5
superimposeLine :: String -> String -> String
superimposeLine (x:xs) (y:ys)
| xs ==[] || ys == [] = [(superimposeChar x y)]
| otherwise = ( superimposeChar x y) : (superimposeLine xs ys)
--Exercise 6.4
superimposeChar :: Char -> Char -> Char
superimposeChar '.' '.' = '.'
superimposeChar _ _ = '#'
--Exercise 6.3
--Exercise 6.2
--Exercise 6.1
mySnd :: (Int,Int)-> Int
mySnd (x,y) =y
sing :: Int -> [Int]
sing x = [x]
--Exercise 5.34
--Exercise 5.33
--Exercise 5.32
--Exercise 5.31
--Exercise 5.30
--Exercise 5.29
returnLoan :: Database -> Person -> Book -> Database
returnLoan database person book = [pair |pair<-database,pair /= (person,book)]
--Exercise 5.28
numBorrowed :: Database->Person -> Int
numBorrowed database p = length (borrowed database p)
borrowers::Database -> Book ->[Person]
borrowers database b = [person| (person,book)<- database,book==b]
borrowed :: Database->Person -> [Book]
borrowed database p = books database p
--Exercise 5.27
books :: Database -> Person -> [Book]
books database p = [book| (person,book)<- database,person==p]
type Person = String
type Book = String
type Database = [(Person,Book)]
exampleBase :: Database
exampleBase =[
("Alice","Tintin"),
("Anna","Little Women"),
("Alice","Asterix"),
("Rory","Tintin")]
Haskell函数式编程基础习题(4)
最新推荐文章于 2020-05-20 23:04:57 发布