Round 1: 旧时的纪录内容
主要是对老师和课程的介绍,加上少许对FP的recall,没太多新的知识,不过发现自己有很多缺失:
- first class:
- undefined:
- lazy evaluation
- id function
有时间要补上
Round 2:
一晃一年了已经,又是AFP的季节~~过去的一年,关于Haskell的编程做的并不多,大部分时间花在增强理论知识上。很庆幸有机会再次学习,可以体验一下这一年自己的变化……
Lecture Notes 跟去年的讲义内容差不多,highlights:
1. (How do Primitive functions and pattern matching achieve, compiler level)
Main> strange undefined
Program error : undefined
An argument is evaluated when a pattern match occurs. But also primitive functions evaluate the arguments.
2.
Bindings are evaluated at most once in their scope.
Top level ones are really evaluated at most once.
3.
group :: Int -> [a] -> [[a]]
group n = takeWhile (not . null)
. map (take n)
. iterate (drop n)
--iterate :: (a -> a) -> a -> [a] -- Defined in GHC.List
*Main> group 3 [1..16]
[[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16]]
In the heap, there should be only one [1,2,...], all sub lists created by iterate (drop n) just some new pointers point to the original one.
*Main> drop 3 [16]
[]
*Main> drop 3 []
[]
So after [16], the iterate (drop n) will keep on creating []. Therefore, this function will create a infinity list which is
[
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],
[4,5,6,7,8,9,10,11,12,13,14,15,16],
[7,8,9,10,11,12,13,14,15,16],
[10,11,12,13,14,15,16],
[13,14,15,16],
[16],
[],
[],
...
*Main> take 3 [16]
[16]
*Main> take 3 []
[]
map (take n) does the job to remove unnecessory elements from each list, it return something like
[
[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12],
[13,14,15],
[16],
[],
[],
...
*Main> :i null
null :: [a] -> Bool -- Defined in GHC.List
*Main> null []
True
*Main> null [1]
False
takeWhile (not . null) could remove all redundant empty lists at the end.
4. In C++, keyword 'concept' is just like 'class' in Haskell
Code Example
Reading
- The World: Ch. 1-6, Ch. 8-10 (mostly repetition of intro. FP).
- Optional: Applicative Functors are introduced in RWH10, used more later in RWH16
- Optional: The Craft: Chapter 12 on overloading, Ch. 16 on abstract types, Ch. 17 on laziness
- Optional: The School: Chapter 12 on type classes, Ch. 14 on streams, Section 18.1 on higher-order types
Related paper
- Why Functional Programming Matters by John Hughes.