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

import Data.Char
---------------------------
--Exercise 5.26
--Exercise 5.25
--Exercise 5.24
--Exercise 5.23
duplicate :: String -> Int -> String
duplicate s 1 = s
duplicate s n = s  ++ (duplicate s (n-1))


--Exercise 5.22
onSeparateLines :: [String] ->  String
onSeparateLines (x:xx) = x ++ (onSeparateLines xx)
onSeparateLines [] = []


--Exercise 5.21
matches :: Int -> [Int] -> [Int]
matches n xs = [x | x <- xs , x ==n ]

myElem ::Int -> [Int] -> Bool
myElem n xs = length (matches n xs) /= 0


--Exercise 5.20
isPrime :: Int -> Bool
isPrime n 
	| n == 1 = False
	| otherwise = length ( divisors n) == 2

divisors :: Int -> [Int]
divisors n = 1: myDivisors n 2

myDivisors::Int -> Int -> [Int]
myDivisors m n 
	| n <=1  =  [1]
	| n >= m = [m]
	| m `mod` n == 0  = n : (myDivisors m (n+1))
	| otherwise = myDivisors m (n+1)
	


--Exercise 5.19-1
capitalizeLetters :: String->String
capitalizeLetters xs = [myIsChar x | x <- xs ]

myIsChar :: Char -> Char
myIsChar c 
	| ((ord 'a') <= (ord c)) && ((ord c) <= (ord 'z')) = ' '
	| otherwise = c
	
--Exercise 5.19
capitalize :: String -> String 
capitalize xs = [myToUpper x | x <- xs ]


myToUpper :: Char -> Char
myToUpper c
	| ((ord 'a') <= (ord c)) && ((ord c) <= (ord 'z')) = chr (ord c -((ord 'z') - (ord 'Z')))
	| otherwise = c
	


--Exercise 5.18
doubleAll :: [Int] ->[Int]
doubleAll xs = [x*2 | x <-xs]

--Exercise 5.17

--Exercise 5.16

--Exercise 5.15

--Exercise 5.14

--Exercise 5.13
isOverlap :: NewShape -> NewShape -> Bool
isOverlap s1 s2 = s1 == s2


--Exercise 5.12
data NewShape = NewShape Float Float Shape
					deriving (Show,Eq)

move :: Float -> Float -> NewShape -> NewShape
move dx dy (NewShape x y shape)= NewShape (x+dx) (y+dy) shape

--Exercise 5.11

--Exercise 5.10

--Exercise 5.9

--Exercise 5.8
isRegular :: Shape -> Bool 
isRegular (Circle _) = True
isRegular ( Rectagle width height) 
	| width == height = True 
	| otherwise = False
isRegular _ = False

--Exercise 5.7
isRound :: Shape -> Bool
isRound (Circle _) = True
isRound _ = False

area :: Shape -> Float
area (Circle radiu) = 3.14 * radiu * radiu
area ( Rectagle width height) = width * height
area _ = 0

--Exercise 5.6
type Name = String
type Weight = String
type Price = Float 

data Item = Item Name Weight Price

--Exercise 5.5
data Shape = Circle Float 
			| Rectagle Float Float
			| Triangles Float Float Float 
			deriving (Eq,Ord,Show)
			
			
perimeter:: Shape -> Float
perimeter (Circle radiu) = 2* 3.14 * radiu 
perimeter (Rectagle width height) = 2 * (width + height)

--Exercise 5.4

--Exercise 5.3
isCrossXAxis :: (Int,Int) -> Bool
isCrossXAxis sl 
		| (fst sl) /= 0  = True
		| otherwise  = False
--Exercise 5.2

--Exercise 5.1
maxOccurs :: Int -> Int ->(Int,Int)
maxOccurs m n 
	| m == n  =  (m,2)
	| m > n = (m,1)
	| otherwise = (n,1)
	
maxThreeOccures:: Int -> Int -> Int -> (Int,Int)
maxThreeOccures x y z 
	| x ==  (fst tempMax) = (x ,1 + (snd tempMax) )
	| x< (fst tempMax) = tempMax
	| otherwise = (x,1)
	where tempMax = maxOccurs y z 

--Exercise 4.39
--Exercise 4.38
--Exercise 4.37
--Exercise 4.36
--Exercise 4.35
--Exercise 4.34
--Exercise 4.33
--Exercise 4.32
myPower :: Int -> Int -> Int 
myPower num mi 
	| mi == 1 = num
	| mi == 2  = num * num
	| mi `mod` 2 == 0  = numbSpeed * numbSpeed 
	| otherwise  = 	num * (myPower num (mi -1))				
		where  numbSpeed = myPower num (mi `div` 2)

--Exercise 4.31
commonFactor :: Int -> Int -> Int
commonFactor m n = myCommonFactor m n (m+n - max m n )

	
myCommonFactor :: Int -> Int -> Int -> Int
myCommonFactor m n fac 
	| fac == 1 = 1
	| (m `mod` fac == 0)   &&  (n `mod` fac == 0)  = fac
	| otherwise = myCommonFactor m n (fac-1)

--Exercise 4.30

--Exercise 4.29
myCube :: Int -> String 
myCube n = (myLeft2Rgith n n ++ myRight2Left n n) `above` (myRight2Left n n ++ myLeft2Rgith n n) 

--Exercise 4.28
myRight2Left :: Int -> Int  -> String 
myRight2Left m n  
	|	n <= m = myBlackWhite m (m-n) `above`  myLeft2Rgith m (m-n+1)
	| 	otherwise =""


--Exercise 4.27
myLeft2Rgith :: Int -> Int  -> String 
myLeft2Rgith m n  
	|	n>0 = myBlackWhite m n `above`  myLeft2Rgith m (n-1)
	| 	otherwise =""
myBlackWhite:: Int -> Int -> String
myBlackWhite total index
	| index == total =  "1" ++ myBlackWhite (total -1) index
	| total <= 0  = ""
	| otherwise = "0" ++ myBlackWhite (total -1) index


--Exercise 4.26
above::String ->String -> String 
above m n = m ++"\n" ++ n


cloumn :: String -> Int -> String
cloumn s n 
	| n <=1  = s
	| otherwise = s `above` (cloumn s (n-1))


--Exercise 4.25
beside :: String -> String -> String 
beside n m = m ++ n ++ m
blackSquares :: Int -> String
blackSquares n
	| n <=1    = "1"
	| otherwise = "1" `beside` blackSquares (n-1)
	
whiteBlack :: Int -> String 
whiteBlack n
	| n <=1  = "0"
	| otherwise = "0" `beside` whiteBlack (n-1)

	
blackWhite :: Int -> String
blackWhite n 
	| n <=1  = "1"
	| otherwise = "1" `beside` whiteBlack (n-1)

--Exercise 4.24
	
--Exercise 4.23
regions :: Int -> Int 
regions n  = sumFun (\x->x) n

sumFun :: (Int->Int) -> Int ->Int
sumFun f n
	| n == 0 = f 0
	| n > 0 = sumFun f (n-1) + f n 

--Exercise 4.22
myBoolF :: Int -> Bool
myBoolF n = boolF f n

boolF :: (Int -> Int) -> Int -> Bool
boolF f n 
	| f n ==0  = True
	| n == 0 = False
	| otherwise = boolF f (n-1)

--Exercise 4.21
myMaxF :: Int -> Int 
myMaxF n = maxF f n

maxF :: (Int -> Int) -> Int -> Int
maxF f n 
	| n == 0 = f n
	| otherwise = max (f n) (maxF f (n-1))


f :: Int -> Int
f 0 = 0
f 1 = 44
f 2 = 17
f _ = 0

--Exercise 4.20
intSquareRoot ::Int -> Int 
intSquareRoot n 
		| n == 1 =1
		| n == 2 = 1
		| n == 3 = 1
		| otherwise = nearNumber n (n `div` 2)  

nearNumber :: Int -> Int -> Int 
nearNumber	n t  
	| n < t*t = nearNumber n (t-1)
	| otherwise = t
		
--Exercise 4.19
multipNautureNumber:: Int->Int
multipNautureNumber n 
	| n == 1  = 1
	| n > 1 = n * multipNautureNumber(n-1)
	| otherwise = 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Haskell函数式编程基础是一本关于Haskell编程语言的基础教程。Haskell是一种纯函数式编程语言,具有强大的类型系统和丰富的函数组合能力。这本PDF书籍主要介绍了Haskell的基本语法、函数定义和应用、递归和高阶函数等概念和技巧。 首先,该书从Haskell的基本语法开始介绍,包括如何定义变量、函数和类型。作者通过简单的示例代码演示了Haskell的函数式特性,帮助读者理解和掌握函数式编程的基本概念。 然后,该书详细介绍了Haskell的函数定义和应用。读者将学习如何定义函数,包括函数的参数和返回值。同时,该书还介绍了函数的局部定义和模式匹配等技巧,这些技巧可以帮助读者更好地组织和重用代码。 接着,该书介绍了Haskell中的递归和高阶函数。递归是函数式编程中一种重要的循环结构,该书通过具体的例子演示了如何使用递归实现一些常见的算法和问题。另外,高阶函数是Haskell的特色之一,该书详细介绍了如何使用高阶函数来简化和抽象代码。 最后,该书还介绍了Haskell的类型系统和类型推导。Haskell是一种静态类型语言,具有强大的类型推导能力,可以帮助程序员在编写代码时避免一些常见的错误。该书通过一些实例详细讲解了Haskell的类型系统,帮助读者理解并正确使用类型。 总之,Haskell函数式编程基础是一本系统而详细的Haskell教程,适合有一定编程基础的读者学习和实践。通过阅读并练习该书中的例子,读者可以逐步掌握Haskell的基本概念和技巧,并能够编写和理解复杂的函数式代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值