其他分享
首页 > 其他分享> > IncRe[2] CTM 1 Introduction to Programming Concepts[0]

IncRe[2] CTM 1 Introduction to Programming Concepts[0]

作者:互联网

目录
本篇前置:

基础信息

1 Introduction to Programming Concepts

1.1 - 1.3 A calculator, Variables, Functions

declare
 V = 1
 W = t(V 2*V)
declare
 V = 2
 X = t(V 2*V)
{Show [V W X]}

输出结果为
[2 t(1 2) t(2 4)]
注:如果不写第二个declare,就会报错。因为这是尝试第二次赋值,而不是“重新declare”。这不允许!

1.4 - 1.5 Lists, Functions over lists

1.6 - 1.7 Correctness, Complexity

the price of correctness is eternal vigilance

想把指数时间复杂度的变为了多项式时间复杂度:

else L in
 L={FastPascal N-1}
 {AddList {ShiftLeft L} {ShiftRight L}}
end

这里的L in声明使得elseend可以使用它,加快运算,避免重复计算L

1.8 Lazy evaluation

Eager/lazy, data-driven/demand-driven
某种程度上相当于数学做题里的“设而不求”。比如

declare
fun lazy {Ints N}
 N|{Ints N+1}
end
declare
 L = {Ints 3}
{Show L}
{Show [L.1 L.2.1 L.2]}

输出

L<optimized>
[3 4 4|_<optimized>]

可以看到相当于先把无穷多个数设出(理论上都available,但是实际中不都取用。取到哪,算到哪)
如果不加lazy
输出

*** Warning: Mozart: virtual memory exhausted.


Process Oz Emulator exited abnormally with code 1

lazy还有一个特性

The lazy version avoids redoing all this work. It is always ready to continue where it left of

换句话说,占用空间多。计算过的都记录下来

1.9 Higher-order programming

1.10 - 1.11 Concurrency, Dataflow

各部分独立(除非指定了要通信)。比如想计算a*b+c*d,就有两个独立部分
还比如说

thread P in
 P = ...
 {Show P}
end
{Show 99*99}

两部分互相独立,上面thread里可能需要计算很久,这样下面的先输出

What happens if an operation tries to use a variable that is not yet bound? From
a purely aesthetic point of view, it would be nice if the operation would simply
wait. Perhaps some other thread will bind the variable, and then the operation can
continue. This civilized behavior is known as dataflow.

既然有了thread(时间有快有慢),就有可能等别的thread

declare X in
thread {Delay 1000} X=99 end
{Show start} {Show X*X}

马上出start,然后等1000ms,才出现9801
核心:互相独立,互不影响,不改变结果

总结和问答练习

  1. Q: 一般状态下下面程序输出顺序是什么?
declare X in
thread {Delay 1200} X=99 end
{Show X*X}
declare X in
thread {Delay 1100} X=98 end
{Show start} {Show X*X}

A: start96049801. 因为注意每个{Show X*X}中的X只和其前面线程运行结果有关,两者相互独立。特别地,程序中,源码写在前面的线程不一定实际出结果在前面。两者在程序中书写的顺序不本质。

标签:end,thread,Show,Introduction,IncRe,fun,CTM,declare,Op
来源: https://www.cnblogs.com/minor-second/p/15690961.html