《Haskell趣学指南 Learn You a Haskell for Great Good!》-代码实验

doubleMe x = x + x
doubleUs x y = doubleMe x + doubleMe y
doubleSmallNumber x =
    if x>100
    then x
    else
        x * 2

doubleSmallNumber‘ x = (if x>100 then x else x * 2) + 1

boomBangs xs = [if x < 10 then "BOOM!" else "BANG!" | x <- xs, odd x]

length‘ xs = sum [1 | _ <- xs]

removeNonUpperCase st = [c | c <- st, c `elem` [‘A‘..‘Z‘]]

rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2]

addThree x y z = x + y + z

lucky :: Int -> String
lucky 7 = "You are lucky 7"
lucky x = "Sorry, you‘re out of luck, pal!"

addVectors :: (Double,Double) -> (Double,Double) -> (Double,Double)
addVectors (x1,y1) (x2,y2) = (x1+x2, y1+y2)

head‘ :: [a] -> a
head‘ [] = error "can‘t call head on an empty list, dummy!"
head‘ (x:_) = x

head‘‘ :: [a] -> a
head‘‘ xs =
    case xs of
    [] -> error "No head for empty list!"
    (x:_) -> x

tell :: (Show a) => [a] -> String
tell [] = "list is empty!"
tell (x:[]) = "list has 1 elem:" ++ (show x)
tell (x:y:[]) = "list has 2 elem:" ++ (show x) ++ " and :" ++ show y
tell (x:y:_) = "list has more then 2 elem, front 2 is :" ++ (show x) ++ " and :" ++ show y

firstLetter :: String -> String
firstLetter "" = "Empty string!"
firstLetter [email protected](x:_) = "The first letter of [" ++ all ++ "] is:" ++ [x]

bmiTell :: Double -> String
bmiTell bmi
    | bmi <= 18.5 = "You‘re under weight"
    | bmi <= 25.0 = "You‘re supposed normal."
    | bmi <= 30.0 = "You‘re fat!"
    | otherwise = "You‘re a whale, congratulations!"

bmiTell‘ :: Double -> Double -> String
bmiTell‘ weight height
    | weight / height ^ 2 <= 18.5 = "You‘re under weight"
    | weight / height ^ 2 <= 25.0 = "You‘re supposed normal."
    | weight / height ^ 2 <= 30.0 = "You‘re fat!"
    | otherwise = "You‘re a whale, congratulations!"

bmiTell‘‘ :: Double -> Double -> String
bmiTell‘‘ weight height
    | bmi <= 18.5 = "You‘re under weight"
    | bmi <= 25.0 = "You‘re supposed normal."
    | bmi <= 30.0 = "You‘re fat!"
    | otherwise = "You‘re a whale, congratulations!"
    where bmi = weight / height ^ 2

max‘ :: Ord a => a -> a -> a
max‘ x y
    | x < y = y
    | otherwise = x

calcBmis :: [(Double,Double)] -> [Double]
calcBmis xs = [ bmi w h | (w,h) <- xs]
    where bmi weight height = weight / height ^ 2

cylinder :: Double -> Double -> Double
cylinder r h =
    let sideArea = 2 * pi * r * h
        topArea  = pi * r ^ 2
    in  sideArea + 2 * topArea

calcBmis‘ :: [(Double, Double)] -> [Double]
calcBmis‘ xs = [bmi | (w, h) <- xs, let bmi = w / h ^ 2]

describeList :: [a] -> String
describeList ls = "This list is " ++ what ls
    where what [] = "empty."
          what [x] = " a single list."
          what xs = " a longer list."

--递归
maximum‘ :: (Ord a) => [a] -> a
maximum‘ [] = error "maximum‘ of empty list"
maximum‘ [x] = x
maximum‘ (x:xs) = max x (maximum‘ xs)

replicate‘ :: Int -> a -> [a]
replicate‘ n x
    | n <= 0    = []
    | otherwise = x : replicate‘ (n-1) x

take‘ :: (Num i, Ord i) => i -> [a] -> [a]
take‘ n _
    | n <= 0    = []
take‘ _ []      = []
take‘ n (x:xs)  = x : take‘ (n-1) xs

reverse‘ :: [a] -> [a]
reverse‘ [] = []
reverse‘ (x:xs) = reverse‘ xs ++ [x]

elem‘ :: (Eq a) => a -> [a] -> Bool
elem‘ a [] = False
elem‘ a (x:xs)
    | a == x    = True
    | otherwise = a `elem‘` xs

--快速排序
quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) =
    let smallerOrEqual = [a | a <- xs, a <= x]
        larger = [a | a <- xs, a > x]
    in quicksort smallerOrEqual ++ [x] ++ quicksort larger

quicksort‘ :: (Ord a) => [a] -> [a]
quicksort‘ [] = []
quicksort‘ (x:xs) =
    let smallerOrEqual = filter (<=x) xs
        larger = filter (>x) xs
    in quicksort‘ smallerOrEqual ++ [x] ++ quicksort‘ larger

{-
第5章 高阶函数
柯里函数: 本质上,Haskell的所有函数都只有一个参数,我们见过所有
多参数的函数,都是“柯里函数”,柯里函数不会一次性取完所有的参数,
而是在每次调用时只取一个参数,并返回一个一元函数来取下一个参数,
以此类推。我们以部分参数来调用某函数,返回一个部分应用(partial
 application) 函数。如: let f = (max 1 ) in f (-100)
-}
divideByTen :: (Floating a) => a -> a
divideByTen = (/10)

divide200 :: (Floating a) => a -> a
divide200 = (200 /)

--函数执行两次
applyTwice :: (a -> a) -> a -> a
applyTwice f x = f (f x)

zipWith‘ :: (a->b->c) -> [a] -> [b] -> [c]
zipWith‘ _ [] _ = []
zipWith‘ _ _ [] = []
zipWith‘ f (x:xs) (y:ys) = f x y : (zipWith‘ f xs ys)

flip‘ :: (a->b->c) -> b -> a -> c
flip‘ f y x = f x y

--map,filter

{-通过foldl函数进行左折叠,它从列表的左端开始折叠,用初始值
和列表的头部调用二元函数,得到一个新的累加值,并用新的累加值与列表
的下一个元素调用二元函数,依次类推-}
sum‘ :: (Num a) => [a] -> a
sum‘ xs = foldl (\acc x -> acc + x) 0 xs

{- "折叠":  foldl1与foldr1的行为与 foldl和foldr相似,
只是无需要明确提供初始值-}
reverse1‘ :: [a] -> [a]
reverse1‘ = foldl (\acc x -> x : acc) []

reverse1‘‘ :: [a] -> [a]
reverse1‘‘ = foldl (flip (:)) []

product‘ :: Num a => [a] -> a
product‘ = foldl (*) 1

{-扫描: scanl,scanr和foldl,foldr相似,不过它们会将累加值的所有变动
记录到一个列表中。也有 scanl1,scanr1与折叠foldl1,foldr1类似-}

--自然数平方根之和累加,到哪个数时,累加值超过1000?
sqrtSums :: Int
sqrtSums = length(takeWhile (<1000) (scanl1 (+) (map sqrt [1..]))) + 1
时间: 2024-10-20 23:41:40

《Haskell趣学指南 Learn You a Haskell for Great Good!》-代码实验的相关文章

【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

译者地址:[翻]Android Design Support Library 的 代码实验--几行代码,让你的 APP 变得花俏 原文:Codelab for Android Design Support Library used in I/O Rewind Bangkok session--Make your app fancy with few lines of code 原文项目 demo: Lab-Android-DesignLibrary 双语对照地址: [翻-双语]Android D

Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

译者地址:[翻]Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏 原文:Codelab for Android Design Support Library used in I/O Rewind Bangkok session----Make your app fancy with few lines of code 原文项目 demo: Lab-Android-DesignLibrary 双语对照地址: [翻-双语]Android

【转】【翻】Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏

转自:http://mrfufufu.github.io/android/2015/07/01/Codelab_Android_Design_Support_Library.html [翻]Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏 Jul 1, 2015 译者地址:[翻]Android Design Support Library 的 代码实验——几行代码,让你的 APP 变得花俏 原文:Codelab for Androi

阿里云抢月饼代码实验,自己写着玩的

阿里4名工程师因刷月饼被开除.软件抢票.优惠秒杀...各种抢. 但是我还不知道怎么抢,所以我就模拟了一下抢月饼的流程. ps:自己不是专业前端,纯属娱乐!!! 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>阿里云抢月饼代码实验</title> 6 <script src="jquery-1.11.3.min

(Android第一行代码实验一)活动的最佳实践

活动的最佳实践    1.知晓当前是在哪一个活动         这个技巧将教会你,如何根据程序当前的界面就能判断出这是哪一个活动.  首先需要新建一个 BaseActivity 继承自 Activity,然后在 BaseActivity中重写 onCreate()方法.         public class BaseActivity extends Activity {                @Override              protected void onCreat

Thrift-0.9.3 代码实验

如果采用TThreadedSelectorServer模型的话. 整个网络IO模型基本上就是 Accept Thread Group --- Selector Thread Group --- ExecutorService 假设第2层的Selector拿到一个socket,并且读取了完整的数据,会抛给ExecutorService. 这是通过 /** * Do the work required to read from a readable client. If the frame is *

代码实验

using System; using System.Runtime.InteropServices; using Autodesk.AutoCAD.Interop; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Runtime;

Java程序设计 实验三

北京电子科技学院(BESTI) 实     验    报     告 课程:Java程序设计   班级:1353       姓名:郭皓  学号:20135327 成绩:             指导教师:娄嘉鹏      实验日期: 实验密级:         预习程度:             实验时间: 仪器组次:          必修/选修:选修       实验序号:3 实验名称:                敏捷开发与XP实践 实验目的与要求: 完成实验.撰写实验报告,实验报告以

实验--反汇编一个简单的C程序(李鹏举)

  攥写人:李鹏举  学号:20132201 ( *原创作品转载请注明出处*) ( 学习课程:<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ) 实验部分(以下命令为实验楼64位Linux虚拟机环境下适用,32位Linux环境可能会稍有不同) 使用gcc –S –o main.s main.c -m32命令编译成汇编代码. 代码如下: int g(int x) { return x + 8; } int f(i