一点点探索cryptol内置函数的过程:
多项式:
pmult 乘
Cryptol> pmult <| x^^3 + x^^2 + x + 1 |> <| x^^2 + x + 1 |>
45
Cryptol> <| x^^5 + x^^3 + x^^2 + 1 |>
45
pdiv 商
Cryptol> pdiv <| x^^5 + x^^3 + 1 |> <| x^^3 + x^^2 + 1 |> == \
<| x^^2 + x |>
True
pmod 余数
Cryptol> pmod <| x^^5 + x^^3 + 1 |> <| x^^3 + x^^2 + 1 |> == \
<| x^^2 + x + 1 |>
True
序列:
take : {front, back, a} (fin front) => [front + back]a -> [front]a
取前3个元素
Cryptol> take`{3}[1 .. 100]
[1, 2, 3]
drop : {front, back, a} (fin front) => [front + back]a -> [back]a
获取去除前三个元素的剩余序列
Cryptol> drop`{3} [1 .. 12]
[4, 5, 6, 7, 8, 9, 10, 11, 12]
# : {front, back, a} (fin front) => [front]a -> [back]a -> [front + back]a
Cryptol> [1 .. 3] # [5 .. 6]
[1, 2, 3, 5, 6]
join : {parts, each, a} (fin each) => [parts][each]a -> [parts * each]a
相对于split而言的,是合起来
Cryptol> join [[1 .. 3], [4 .. 6]]
[1, 2, 3, 4, 5, 6]
split : {parts, each, a} (fin each) => [parts * each]a -> [parts][each]a
分成三组
Cryptol> split`{3}[1 .. 12]
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
groupBy : {each, parts, a} (fin each) => [parts * each]a -> [parts][each]a
分组,每一组三个元素
Cryptol> groupBy`{3} [1 .. 12]
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
transpose : {rows, cols, a} [rows][cols]a -> [cols][rows]a
Cryptol> transpose [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
reverse : {n, a} (fin n) => [n]a -> [n]a
Cryptol> reverse [1 .. 12]
[12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
head : {n, a} [1 + n]a -> a
取头元素
Cryptol> head [1 .. 12]
1
tail : {n, a} [1 + n]a -> [n]a
去掉头元素
Cryptol> tail [1 .. 12]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
last : {n, a} (fin n) => [1 + n]a -> a
Cryptol> last [1 .. 12]
12
sum:序列求和
Cryptol> sum [1 .. 100]
5050
repeat:
Cryptol> repeat [1 .. 5]
[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5],
[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], ...]
Cryptol> repeat 5
[5, 5, 5, 5, 5, ...]
length : 求序列的长度
Cryptol> length [1 .. 12]
12
elem:判断一个元素是否是一个序列的子元素
Cryptol> elem 1 [1 .. 12]
True
and \ or:
Cryptol> and [True, False, True]
False
scanl \ scanr:
Cryptol> scanl (+) 2 [1 .. 5]
[2, 3, 5, 8, 12, 17]
Cryptol> scanr (+) 2 [1 .. 5]
[17, 16, 14, 11, 7, 2]
zip:
Cryptol> :set ascii=on
Cryptol> zip [1 .. 3] "fin"
[(1, 'f'), (2, 'i'), (3, 'n')]
zipWith:
Cryptol> zipWith (+) [1 .. 4] [5 .. 8]
[6, 8, 10, 12]
Cryptol> zipWith (-) [1 .. 4] [5 .. 8]
[-4, -4, -4, -4]
foldl:
foldl (-) 2 [1 .. 3] = (2-1) + (2-2) +(2-3]
foldr (-) 2 [1 .. 3] = (3-2) + (2-2) + (1-2)
Cryptol> foldl (-) 2 [1 .. 3]
-4
Cryptol> foldr (-) 2 [1 .. 3]
0