Lecture 1

Round 1: 旧时的纪录内容


主要是对老师和课程的介绍,加上少许对FP的recall,没太多新的知识,不过发现自己有很多缺失:

  1. first class:
  2. undefined:
  3. lazy evaluation
  4. 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

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值