• take和 drop功能还有takewhile 和 dropwhile 功能主要用于分开list


    take和 drop功能还有takewhile 和 dropwhile 功能主要用于分开list

    1. 首先所有的Haskell的功能名得小写,其次像mod 和 div 这种运算符号得用而不是'' (这个符号是在笔记本电脑~下)
    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    (reverse 本身的确是将list 翻转, 但是由于这个公式在进行比较,不仅是=;/=;>;<等都会得到一个Bool的结果.因此这个公式的type最后一定是Bool)

    **twice :: (t -> t) -> t -> t
    twice f x = f (f x)
    
    • 1
    • 2

    (f 代表了功能function, 可以输入double;swap等,这个公式会重复运行这个功能两次)

    halve :: [a] -> ([a], [a])
    halve xs = (take n xs, drop n xs)
        where n = (length xs) `div` 2
    
    • 1
    • 2
    • 3

    (这个公式是为了将list 通过长度而一分为二;但不适用于string)

    split1 :: Int -> String -> [String]
    split1 len xs 
      | len == length xs = [xs]
      | otherwise = take len xs : split1 len (drop len xs)
    
    • 1
    • 2
    • 3
    • 4

    (这个公式是用于string)

    firstone :: String -> String
    firstone input
        = takeWhile (/= ' ') (dropWhile (== ' ') input)
    
    • 1
    • 2
    • 3

    (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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    本身的tail是不包含[] 和只有一个元素的情况,这里有三种不同的方式来让tail 可以包含所有的情况

    stack :: [a] -> [a]
    stack (x:xs) = xs ++ [x]
    
    • 1
    • 2

    将一个list的第一个item放到整个list的最后

    com :: [a] -> [a] -> [a]
    com (x:xs) bs = bs ++ [x]
    
    • 1
    • 2

    将两个不同的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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    记住在termination运行时char是单引号!!

    halves :: Fractional a => [a] -> [a]
    halves ns = map (/2) ns
    
    halves :: Integral a => [a] -> [a]
    halves ns = map (`div` 2) ns
    
    • 1
    • 2
    • 3
    • 4
    • 5

    halves 接受一个列表并将列表中的每个元素除以二

    halves :: Fractional a => [a] -> [a]
    halves ns = map (/2) ns
    
    halves1 :: Integral a => [a] -> [a]
    halves1 ns = map (`div` 2) ns
    
    • 1
    • 2
    • 3
    • 4
    • 5

    第一个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'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    首字母大写

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    bash: /usr/bin/cp: Argument list too long Linux 系统大量数据移动复制出错
    Android 单一音量控制
    Flutter 离线数据方案 Flutter_Data 包
    Python中将字典转为成员变量
    kotlin修饰符const的含义
    【Redis】缓存更新策略
    利用HbuilderX制作简单网页: HTML5期末大作业——html5漫画风格个人主页
    Java --- JVM虚拟机栈
    火山引擎VeDI:如何高效使用A/B实验,优化APP推荐系统
    HNUCM-2022年秋季学期《算法分析与设计》练习15
  • 原文地址:https://blog.csdn.net/kirsten111111/article/details/126397712