Introduction to Basic Haskell Components (In Chinese)ChengHui Weng
In 2012, we had the first Chinese functional meetup about general functional programming techniques in Taipei. I gave this talk to introduce several classes in the famous Typeclassesopedia article.
My inspiration from reading *C++ Primer*, *Effective C++*, *More Effective C++*, *The C++ Standard Library* and some experience from coding.
Include:
* Debug
* C++ Syntax
* Habit && Optimization
* Trick
* Trap
* Reference
Introduction to Basic Haskell Components (In Chinese)ChengHui Weng
In 2012, we had the first Chinese functional meetup about general functional programming techniques in Taipei. I gave this talk to introduce several classes in the famous Typeclassesopedia article.
My inspiration from reading *C++ Primer*, *Effective C++*, *More Effective C++*, *The C++ Standard Library* and some experience from coding.
Include:
* Debug
* C++ Syntax
* Habit && Optimization
* Trick
* Trap
* Reference
This is my presentation for emacs, it contains almost all the basic keys and basic featuers. This slides is created by beamer, you can find the source code at https://github.com/alex8866/Beamer/tree/master/Alex
Since the previous LTS, JDK 17, was released in 2021, there are quite lots of features added into the Java specification. In this year, the latest JDK 21 LTS version is released and brings us many formal functions to improve our daily works. This session will focus on the new features, including String Templates, Sequenced Collections, Record Patterns, Pattern Matching for switch, Unnamed features and Virtual Threads.
4. File: /home/aaron/Desktop/a Page 4 of 18
====== 第三章 ======
Created Thursday 20 June 2013
格式化列表一般寻则元组
>>> format = "hello, %s,%s enough for ya?"
>>> values = ('world','hot')
>>>print format % values
Hello, Wrold Hot enough for ya?
如果使用列表或者其他序列替代元组, 那么序列就会被解释为一个值,只有元组和字典可以格式化一个以上
的值
格式转换,基本的转换说明符包括一下部分:
1. %字符: 标记转换说明的开始
2. 转换标致(可选) -表示左对齐;+表示在转换值之前要加上正负号
""空白字符,表示证书之前保留空格0表示转换值若位数不够则用0填充
3. 最小字段宽度: 转换后的字符串至少应该具有该值制定的宽度,如果是×则宽度会从值元组中读出
4. (.) 后跟精度值
5. 转换类型
...
字符串方法:
字符串的方法还要丰富的多,这是因为字符串从string模块中继承了很多方法
find() 返回字串所在位置的最左端的索引.如果没有找到则返回-1
join方法是非常重要的字符窜方法,它是split方法的逆方法,用来在队列中添加元素
lower()
replace()
eg:
>>> 'This is a test'.replace('is', 'eez')
split这是一个非常重要的字符串方法,用来将字符串分割成序列
eg:
'1+2+3'.spilt('+') 如果不提供任何分隔符,程序会把所有的空格作为分隔符(空格,制表,换行等)
strip方法去除两侧空格的字符串
它和lower方法一起使用的话就可以很方便的对比输入的和存储的值 very good
translate方法和replace方法一样,可以替换字符串中的某些部分,但是和前者不同的是该方法只处理单个字
符,他的优势在于可以同时进行多个替换,有时候比replace效率更高的多
Content-Type: text/x-zim-wiki
Wiki-Format: zim 0.4
Creation-Date: 2013-06-20T15:06:51+08:00
====== 第四章 ======
Created Thursday 20 June 2013
字典!!!
>>>items=[('name', 'Gumby'), ('age', 43)]
5. File: /home/aaron/Desktop/a Page 5 of 18
>>>d = dict(items)
>>>d
{'age': 42, 'name': 'Gumby'}
copy方法返回一个具有相同兼职对的新字典
fromkeys 方法使用给定的键建新的字典,每个剑默默认的自己为None
Content-Type: text/x-zim-wiki
Wiki-Format: zim 0.4
Creation-Date: 2013-06-20T16:25:06+08:00
====== 第五章 ======
Created Thursday 20 June 2013
print 'Age:',42
from math import sqrt as foobar
多个赋值同时进行
>>> x,y,z = 1, 2, 3
>>>print x,y,z
1 2 3
x,y = y,x 交换2个变量, 事实上这里所作的事情叫做序列解包-将多个值序列解开,然后放到变量的序列中,
当函数或者方法返回元组时,这个特性尤其有用,假设需要获取(和删除)字典中任意的键值对,可以使用
popitem方法,这个方法将键值作为元组返回
>>>key,value = scound.popitem()
>>key
1
>> value
2
Python中所有的值都能被解释成真值
bool函数可以用来将其他值转换成bool值
if语句
if name.endswith('Gumby'):
print ...
elif num < 0:
print ...
else:
print ...
elif 和shell表现一样 但和awk表现不一样 awk和c一样 else if
1. 比较运算符
2. x == y, x != y, x is y x和y是同一个对象, x is not y x和y 是不同的对象,x in y, x not in y
3. 0<age<100 在python是可以的但在c中是不可以的
if 's' in name:
print ...
bool运算符
6. File: /home/aaron/Desktop/a Page 6 of 18
c: && ||
awk: && ||
shell: -o, -a
python: and or not
循环
while 循环
x =1
while x<= 100:
print x;
x += 1;
while not name:
....
words = ['this', 'is', 'an', 'ex']
for word in words:
print word
因为迭代(循环的另外一种说法)某范围的数字是很常见的,所以有个内建的范围函数供使用:
>>> range(0, 10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
eg:
for number in range(0,10):
print ...
循环遍历字典元素
for key in d:
print key,d[key]
for key, value in d.item():
print key,value
一些迭代工具:
1. 并行迭代
names=['a','b']
ages=[1,2]
for i in range(len(names)):
print names[i],ages[i]
zip(names,ages)
返回:[('a',1),('b',2)] 参考{‘a':1, 'b':2}
for name,age in zip(names,ages):
print name, is age
2. 编号迭代
for index, string in enumerate(strings):
if 'xxx' in string:
strings[index] = '[censored]'
from math import sqrt
for n in range(99,81,-1):
root = sqrt(n)
if root == int(root):
print n
break
else:
print ...
上面的else仅在没有调用break时执行
7. File: /home/aaron/Desktop/a Page 7 of 18
列表推导 — 轻量级循环
列表推导是利用其他列表创建新列表的一种方法,它的工作方式类似于for循环,也很简单
eg:
>>> [x*x for x in range(10)]
[0,1,4,9,...,81]
这个语句可以通过增加一个if部分添加到列表推导中eg:
[ x*x for x in range(10) if x%3 == 0 ]
[(x,y) for x in range(3) for y in range(3)]
[(0,0), (0,1),(0,2) ... ]
pass什么都不做
del 他不仅会移除一个对象的引用,也会移除那个名字本身
使用exec和eval执行和求值字符串
exec "print 'hello world'"
本章的函数
chr(n) 当传入序号时,返回n所代表的包含一个字符的字符串
eval()
enumerate(seq)
ord(c)返回单字符字符串的int值
range()
reversed(seq)
sorted
xrage
zip
== 哈哈 到这里基本知识已经学完了 ==
Content-Type: text/x-zim-wiki
Wiki-Format: zim 0.4
Creation-Date: 2013-06-20T18:05:15+08:00
====== 第六章 ======
Created Thursday 20 June 2013
参数parameter 作用域:scope
__创建函数__
eg:
''def hello(name):''
''return 'Hello, ' + name +'!'''
文档字符串
''def square(x):''
'calculates the square of the number x.'
return x*x
8. File: /home/aaron/Desktop/a Page 8 of 18
文档字符串可以按如下方式访问:
>>> square.__doc__
>>> help(square)
__参数魔法:__
当两个变量同时引用一个__列表__时,他们的确是同时引用一个__列表,当在序列中做切片的时候 返回的切
片总是一个副本__
位置参数在调用时是不传入参数名字的
关键字参数ge: hello_2(greeting='hello',name='world')
收集参数:
def print_params(*params)
参数前面的*号将所有值放置在同一个元组中。可以说是将这些值收集起来,然后使用。*号意思是收集其余
参数
*收集元组 **收集字典
eg:
def print_params_4(x, y, z = 3, *pospar, **keypar):
print x, y, z
print pospar
print kdypar
>>> print_params_4(1,2,3,4,5,6,7,foo = 1,bar = 2)
1 2 3
(5,6,7)
{'foo':1, 'bar':2}
作用域
若变量同名 ,想访问全局变量需要:
print parameter + globals()['parameter']
def change_global():
global x
x = x + 1
本章新的函数:
map(func, seq, [, seq, ...]) 对序列中的每个元素应用函数
filter(func, seq) 返回其函数为真的序列列表
reduce(func, seq [, initial])
sun(seq)
Content-Type: text/x-zim-wiki
Wiki-Format: zim 0.4
Creation-Date: 2013-06-20T19:16:34+08:00
====== 第七章 ======
Created Thursday 20 June 2013
Python是面向对象的程序设计语言
对象最重要的优点包含以下几方面: