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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章