0%

Programming in Haskell是一本入门Haskell的好书,介绍页面以及配套的slides, vedios, codes都在这里


mapfilter表示[f x | x <- xs, p x]

1
2
3
-- [f x | x <- xs, p x]
-- [f x | x <- xs, p x]
-- = map f (filter p xs)

定义高阶函数all, any, takeWhile, dropWhile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
myall :: (a -> Bool) -> [a] -> Bool
myall _ [] = True
myall p (x : xs) | p x == False = False
| otherwise = myall p xs

-- *Main> myall (>3) [1..3]
-- False
-- *Main> myall (>3) [1..5]
-- False
-- *Main> myall (>3) [4..5]
-- True

myany :: (a -> Bool) -> [a] -> Bool
myany _ [] = False
myany p (x : xs) | p x == True = True
| otherwise = myany p xs

-- *Main> myany (>3) [1..5]
-- True
-- *Main> myany (>3) [1..3]
-- False


mytakeWhile :: (a -> Bool) -> [a] -> [a]
mytakeWhile _ [] = []
mytakeWhile p (x : xs) | p x == True = x : mytakeWhile p xs
| otherwise = []

-- *Main> mytakeWhile (<3) [1..5]
-- [1,2]
-- *Main> mytakeWhile (>3) [1..5]
-- []
-- *Main> mytakeWhile (<3) [1,2,3,1,2]
-- [1,2]

mydropWhile :: (a -> Bool) -> [a] -> [a]
mydropWhile _ [] = []
mydropWhile p (x : xs) | p x == True = mydropWhile p xs
| otherwise = x : xs

-- *Main> mydropWhile (<3) [1..5]
-- [3,4,5]
-- *Main> mydropWhile (>3) [1..5]
-- [1,2,3,4,5]

Read more »

Programming in Haskell是一本入门Haskell的好书,介绍页面以及配套的slides, vedios, codes都在这里


这一章节就是在讲递归,所以下面的定义都是默认用递归定义。

定义幂函数

1
2
3
4
5
6
7
8
9
10
mypow :: Int -> Int -> Int
mypow _ 0 = 1
mypow x n = x * mypow x (n-1)

-- *Main> mypow 2 3
-- 8
-- *Main> mypow 3 0
-- 1
-- *Main> mypow 3 1
-- 3

解释length, drop, init的递归求值过程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

-- length [1, 2, 3]
-- = 1 + length [2, 3]
-- = 1 + (1 + length [3])
-- = 1 + (1 + (1 + length []))
-- = 3

-- drop 3 [1..5]
-- = drop 2 [2..5]
-- = drop 1 [3..5]
-- = drop 0 [4, 5]
-- = [4, 5]

-- init [1..3]
-- = 1 : init [2, 3]
-- = 1 : 2 : init [3]
-- = 1 : 2 : []
-- = [1, 2]

定义以下库函数

Read more »

Programming in Haskell是一本入门Haskell的好书,介绍页面以及配套的slides, vedios, codes都在这里


用List Comprehension计算1到100平方和

1
sum [x^2 | x <- [1..100]]

定义库函数replicate

1
2
3
4
5
-- > replicate 3 True
-- [True, True, True]

myreplicate :: Int -> a -> [a]
myreplicate n x = [x | _ <- [1..n]]

定义函数pyths

pyths n返回满足x^2 + y^2 == z^2的三元组(x, y, z),其中xyz都小于等于n

1
2
3
4
5
6
7
8
-- > paths 10
-- [(3, 4, 5), (4, 3, 5), (6, 8, 10), (8, 6, 10)]

pyths :: Int -> [(Int, Int, Int)]
pyths n = [(x,y,z) | x <- [1..n],
y <- [1..n],
z <- [1..n],
x^2 + y^2 == z^2]
Read more »

Programming in Haskell是一本入门Haskell的好书,介绍页面以及配套的slides, vedios, codes都在这里


定义 halve

1
2
halve :: [a] -> ([a], [a])
halve a = let n = (length a) `div` 2 in (take n a, drop n a)

用三种方式定义safetail

1
2
3
4
5
6
7
8
9
10
11
12
13
14
-- conditional expression
safetail1 :: [a] -> [a]
safetail1 xs = if null xs then xs else tail xs

-- guarded equations
safetail2 :: [a] -> [a]
safetail2 xs | null xs = xs
| otherwise = tail xs

-- pattern matching
safetail3 :: [a] -> [a]
safetail3 [] = []
safetail3 (_:xs) = xs

Read more »

Programming in Haskell是一本入门Haskell的好书,介绍页面以及配套的slides, vedios, codes都在这里


写出这些表达式的type

1
2
3
4
5
['a', 'b', 'c']             -- :: [Char]
('a', 'b', 'c') -- :: (Char, Char, Char)
[(False, 'O'), (True, '1')] -- :: [(Bool, Char)]
([False, True], ['0', '1']) -- :: ([Bool], [Char])
[tail, init, reverse] -- :: [[a] -> [a]]

写出这些函数的type

1
2
3
4
5
6
second xs       = head (tail xs)    -- [a] -> a
swap (x, y) = (y, x) -- (a, b) -> (b, a)
pair x y = (x, y) -- a -> b -> (a, b)
double x = x * 2 -- Num a => a -> a
palindrome xs = reverse xs == xs -- Eq a => a -> Bool
twice f x = f (f x) -- (a -> a) -> a -> a
Read more »

基础准备

安装插件

SublimeHaskell

在Package Control中安装SublimeHaskell。
插件安装完成后,重启Sublime报错,提示需要安装hsdev cabal package。

1
2
3
4
5
SublimeHaskell: hsdev executable couldn't be found!
It's used in most features of SublimeHaskell
Check if it's installed and in PATH
If it's not installed, run 'cabal install hsdev' to install hsdev
You may also want to adjust 'add_to_PATH' setting
Read more »

Programming in Haskell是一本入门Haskell的好书,介绍页面以及配套的slides, vedios, codes都在这里


给算数表达式加上括号

按照运算符优先级就可以,略过。

修改代码,使之正确

length xs用括号括起来即可。

尝试定义last

last: 返回非空list的最后一个元素

1
mylast a = a !! ((length a) - 1)

尝试定义init

init: 删除非空list的最后一个元素,并返回list

1
2
3
myinit1 a = take ((length a) - 1) a

myinit2 a = let n = length a in reverse (tail (reverse a))

问题描述

hexo d命令部署的时候总是会卡住很久,反复尝试了几次后,每次都是control+C告终。

1
2
3
4
5
6
7
▶ hexo d
INFO Deploying: git
INFO Clearing .deploy_git folder...
INFO Copying files from public folder...
On branch master
nothing to commit, working directory clean
^CINFO Bye!
Read more »

Programming in Haskell是一本入门Haskell的好书,介绍页面以及配套的slides, vedios, codes都在这里


给出double (double 2)的另外一种可能解释。

书上给的例子是应用序和正则序(从左向右),可以将正则序(从右向左)当作答案。

1
2
3
4
5
6
7
double (double 2)
= double 2 + double 2
= double 2 + (2 + 2)
= double 2 + 4
= (2 + 2) + 4
= 4 + 4
= 8
Read more »