而不是'' (这个符号是在笔记本电脑~下)second :: [a] -> a
second xs = head (tail xs)
swap :: (b, a) -> (a, b)
swap (x,y) = (y,x)
pair :: a -> b -> (a, b)
pair x y = (x,y)
**double :: Num a => a -> a
double x = x*2
**palin :: Eq a => [a] -> Bool
palin xs = reverse xs == xs
(reverse 本身的确是将list 翻转, 但是由于这个公式在进行比较,不仅是=;/=;>;<等都会得到一个Bool的结果.因此这个公式的type最后一定是Bool)
**twice :: (t -> t) -> t -> t
twice f x = f (f x)
(f 代表了功能function, 可以输入double;swap等,这个公式会重复运行这个功能两次)
halve :: [a] -> ([a], [a])
halve xs = (take n xs, drop n xs)
where n = (length xs) `div` 2
(这个公式是为了将list 通过长度而一分为二;但不适用于string)
split1 :: Int -> String -> [String]
split1 len xs
| len == length xs = [xs]
| otherwise = take len xs : split1 len (drop len xs)
(这个公式是用于string)
firstone :: String -> String
firstone input
= takeWhile (/= ' ') (dropWhile (== ' ') input)
(takeWhile 的 type 是(a -> Bool) -> [a] -> [a] 这意味着当它满足a是True 的情况下才会take.然而take的type却是Int -> [a] -> [a],代表的意思是第一个参数确定应该从作为第二个参数传递的列表中获取多少项.)
tail1 :: [a] -> [a]
tail1 [] = []
tail1 [b] = [b]
tail1 (a : as) = as
tail2 :: [a] -> [a]
taill2 xs | null xs = []
| otherwise = tail xs
tail3 :: [a] -> [a]
tail3 xs = if null xs then [] else tail xs
本身的tail是不包含[] 和只有一个元素的情况,这里有三种不同的方式来让tail 可以包含所有的情况
stack :: [a] -> [a]
stack (x:xs) = xs ++ [x]
将一个list的第一个item放到整个list的最后
com :: [a] -> [a] -> [a]
com (x:xs) bs = bs ++ [x]
将两个不同的list结合起来
range ::Int -> Bool
range x
|x <= 10 && x >= 0 = True
|otherwise = False
range1 :: (Num a, Ord a) => a -> Bool
range1 n = n > 0 && n < 10
range 接受一个数值并检查它是否在 0 到 10 之间,错误为 False ,正确为True
begin1 :: Char -> String -> String
begin1 c str = c:str
begin2 :: Char -> String -> String
begin2 c [] = [c]
begin2 c (y:ys)
|c == y =(y:ys)
|otherwise = [c] ++ (y:ys)
记住在termination运行时char是单引号!!
halves :: Fractional a => [a] -> [a]
halves ns = map (/2) ns
halves :: Integral a => [a] -> [a]
halves ns = map (`div` 2) ns
halves 接受一个列表并将列表中的每个元素除以二
halves :: Fractional a => [a] -> [a]
halves ns = map (/2) ns
halves1 :: Integral a => [a] -> [a]
halves1 ns = map (`div` 2) ns
第一个halves 得出的是小数,第二个得出的是整数
要加限制条件是因为map的括号里是简写所以得用限制条件
capticals :: String -> String
capticals [] = []
capticals (x:xs)
|islower x = toUpper x: xs
|otherwise = (x:xs)
islower :: Char -> Bool
islower c= c >= 'a' && c <= 'z'
首字母大写
odds :: [Int] -> [Int]
odds [] = []
odds (x:xs)
|odd x = x: odds xs
|otherwise = odds xs
odds1 :: [Int] -> [Int]
odds1 xs = filter isOdd xs
where
isOdd n = n `mod` 2 /= 0