doubleMe x = x + x
doubleUs x y = doubleMe x + doubleMe y
doubleSmallNumber x =
if x>
then x
else
x * doubleSmallNumber' x = (if x>100 then x else x * 2) + 1 boomBangs xs = [if x < 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 <- [..], b <- [..c], a <- [..b], a^ + b^ == c^] addThree x y z = x + y + z lucky :: Int -> String
lucky = "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 all@(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 ^ <= 18.5 = "You're under weight"
| weight / height ^ <= 25.0 = "You're supposed normal."
| weight / height ^ <= 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 ^ 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 ^ cylinder :: Double -> Double -> Double
cylinder r h =
let sideArea = * pi * r * h
topArea = pi * r ^
in sideArea + * 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 <= = []
| otherwise = x : replicate' (n-1) x take' :: (Num i, Ord i) => i -> [a] -> [a]
take' n _
| n <= = []
take' _ [] = []
take' n (x:xs) = x : take' (n-) 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 ) in f (-)
-}
divideByTen :: (Floating a) => a -> a
divideByTen = (/) divide200 :: (Floating a) => a -> a
divide200 = ( /) --函数执行两次
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 (<) (scanl1 (+) (map sqrt [..]))) +

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

  1. [2017.02.21] 《Haskell趣学指南 —— Learning You a Haskell for Great Good!》

    {- 2017.02.21 <Haskell趣学指南 -- Learning You a Haskell for Great Good!> [官网](http://learnyouahas ...

  2. [2017.02.21-22] 《Haskell趣学指南 —— Learning You a Haskell for Great Good!》

    {- 2017.02.21-22 <Haskell趣学指南 -- Learning You a Haskell for Great Good!> 学习了Haskell的基本语法,并实现了一 ...

  3. 《Haskell趣学指南》

    <Haskell趣学指南> 基本信息 原书名:Learn You a Haskell for Great Good!: A Beginner's Guide 原出版社: No Starch ...

  4. haskell趣学指南笔记1

    网址:http://learnyouahaskell.com/ 中文版:http://learnyouahaskell-zh-tw.csie.org/zh-cn/ready-begin.html 在 ...

  5. Haskell 趣学指南 入门笔记(二)

    显示类型声明,Haskell是不用定义类型的原因,很像python 想要确定某个表达式的类型 *Main> :t 'a' 'a' :: Char *Main> :t True True : ...

  6. Haskell趣學指南--这个有意思

    正在慢慢了解不同于命令式的函数式语言. 希望能获得新的视野.. ~~~~~~~~~~~ http://learnyouahaskell-zh-tw.csie.org/zh-cn/ready-begin ...

  7. 《趣学Python编程》

    <趣学Python编程> 基本信息 作者: (美)Jason Briggs 译者: 尹哲 出版社:人民邮电出版社 ISBN:9787115335951 上架时间:2014-2-21 出版日 ...

  8. 趣学算法 PDF pdf 下载 陈小玉版

    趣学算法pdf高清无水印版下载 最近在网上找趣学算法pdf,最后还是买了完整版,今天将本书分享出来,分享给那些和我一样在网上苦苦寻找的小可爱们,有条件的话请支持正版! 链接:https://pan.b ...

  9. 入门Python:《趣学Python编程》中英文PDF+代码

    入门python推荐学习<趣学python编程>,语言轻松,通俗易懂,讲解由浅入深,力求将读者阅读和学习的难度降到最低.任何对计算机编程有兴趣的人或者首次接触编程的人,不论孩子还是成人,都 ...

随机推荐

  1. 【c++】标准模板库STL入门简介与常见用法

    一.STL简介 1.什么是STL STL(Standard Template Library)标准模板库,主要由容器.迭代器.算法.函数对象.内存分配器和适配器六大部分组成.STL已是标准C++的一部 ...

  2. JavaScript中toStirng()与Object.prototype.toString.call()方法浅谈

    toStirng()与Object.prototype.toString.call()方法浅谈 一.toString()是一个怎样的方法?它是能将某一个值转化为字符串的方法.然而它是如何将一个值从一种 ...

  3. FFT模板

    我终于下定决心学习FFT了. orzCHX,得出模板: #include<cstdio> #include<cctype> #include<queue> #inc ...

  4. CentOS GO 语言环境的搭建

    go 语言源码安装依赖 ,gcc ,make glibc库,等,上述工具安装省略, 这个是官方地址:http://www.golang.org/ 另外,其源代码更新采用的是mercurial 工具,安 ...

  5. replaceCharactersInRange

    NSString 替换字符串中某一位置的文字  replaceCharactersInRange NSString 替换字符串中某一位置的文字 - (void)viewDidLoad { NSMuta ...

  6. 用openGL实现用黑白相间的棋盘图案填充多边形

    #include<gl/glut.h> #include<windows.h> ; ,b0=,a1=,b1=,a2=,b2=,a3=,b3=; ,winHeight=; voi ...

  7. 你们以为运营商只是HTTP插点广告而已么?

    国内某邮件服务商,近期在某南方地区有大量客户反应登录时出错和异常,于是工作人员进行了一下跟进,发现如下: 首先,邮件服务商登陆页面为普通HTTP协议发送,提交时通过JS进行RSA加密(没错,JS的RS ...

  8. PB 简单笔记!

    1.总体说明: a) 程序不区分大小写 b) 赋值用=  –String city=“南京”,country ;Integer  person[3]={3,8,9};String s = ' You ...

  9. Mininet实验 OpenFlow1.3协议基于Mininet部署与验证

    参照:OpenFlow1.3协议基于Mininet部署与验证 安装过程,参考原文. 实验 使用ifconfig查看本机IP地址:192.168.1.101 进入OpenDayLight界面,cd到bi ...

  10. Javascript 笔记与总结(2-9)获取运行时的 style 对象

    获取内存中(正在渲染)的 style 的值(非内联 style,obj.style 只能获得内联 style 的值),可以用 obj.currentStyle(低版本 IE 和 Opera 支持)和 ...