Haskell函数式编程基础习题(4)

--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")]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值