WEEK3-Lists and pattern matching

Paradigms of Computer Programming - Fundamentals (EDX)
WEEK3-Lists and pattern matching 笔记


1. Lists


Lists informally

One of the first, and most useful, compound data types that was invented is the list. A list is an ordered collection of elements. Lists are a fundamental data type in the symbolic language Lisp (which stands for List Processor), invented in the 1950s. A symbolic language is a language that has special support to calculate with data that are not numbers, for example, lists, trees, graphs, and other non-numeric data.

We can use lists in Oz. The simplest way to create a list is with brackets [ ... ]. For example, X=[1 2 3] is a list of three elements, 1, 2, and 3. We can calculate with lists and display lists with Browse. The call {Append X X} will calculate and return a new list consisting of the elements of its first argument (here X) followed by the elements of its second argument (again X). This call will return [1 2 3 1 2 3].

Note that lists, once created, cannot be changed. Lists are values, i.e., they areconstants. This is a consequence of programming in the functional paradigm. If we need a different list, we cannot change an existing list, instead we must create a new list.


总的来说,就是:oz里也有list,也是加[](用空格分隔),也有append;不过,在oz里,list是不可变的。


Defining lists with grammar rules

We can give a precise definition of a list using recursion. A list is either an empty list (denoted as 'nil' without the quotes) or a pair of an element and another list (both separated by '|' without the quotes). The word 'list' appears both in the head and in the body of this definition. This definition is not circular because the list in the body will always be smaller than the list in the head. We define a list in terms of a smaller list, and so forth, until we define the empty list. This is the same idea that recursive functions use to avoid infinite executions. Here, the idea avoids data structures of infinite size.

关于list的递归:一个list可以是一个空的list(nil),或者一组元素和另一个list;都用|分隔。在第二种情况,list总是比它里面的list大,这样,直到里面的list变成空的,就完成了递归。


We give a precise definition of a list using a formal grammar. The particular grammar we use is an example of EBNF: Extended Backus Naur Form. This is named after John Backus and Peter Naur, two pioneers of programming languages. EBNF is one of the most popular ways of defining the syntax of programming languages. In EBNF, a grammar definition consists of grammar rules. Each grammar rule defines the syntax of one language entity, such as <List T>. The details of the meaning of the rules are explained in the video. One important point is not to confuse the two symbols | and '|'. The former is part of EBNF's own syntax; it means "or". The latter is part of the language entity that we are defining; it means "|" (a vertical bar by itself, with no symbols surrounding it). In the notation <List T>, the T is called a type variable. This should not be confused with identifiers and variables in memory. Type variables exist only in grammar rules, not in programs.

没有引号的|和有引号的'|'是不同的:没引号的表示or,有引号的目前不太理解。


A major subtlety in grammar definitions is not to confuse a thing with its representation. We use the famous painting "Ceci n'est pas une pipe" ("This is not a pipe") by the surrealist painter René Magritte to illustrate this point.

Useful representations for lists

We present two useful representations of lists: syntactic sugar and a graphical representation. The notation [10 11 12] is just another way to write 10 | 11 | 12 | nil. Both notations represent exactly the same list. For the programming system, there is no difference. The first notation exists only as a facility for the programmer. We call it a syntactic sugar.

有两种有用的list的表示法:糖衣语法和图示。[10 11 12]也可以写成10 | 11 | 12 | nil,这种叫做糖衣语法。(老师说像糖一样吃多了不好,但是吃一点也是不错的。)


A list has a natural graphical representation as a binary tree. A tree is a kind of graph, i.e., a figure consisting of nodes, which are geometric points, connected by edges, which are (curved) lines. Formally, a graph is a set of nodes and edges, where an edge is a pair of two nodes.

另一种就是图示,表现为二叉树。(就是下图这个样子。)



(这一小节的练习不让用append我暂时不会,翻了一下老师提供的textbook,结果里面有,用的是下面讲到的case of then else end。)


2. Calculating with lists


Tail recursion for lists


%Calculating with lists piece by piece

 OUTPUT
declare X1 X2 in

X1 = 6|X2


{Browse X1}

6|_
declare X3 in
X2 = 7|X3
6|7|_
X3 = nil6|7|nil
{Browse X1}[6 7]
{Browse [6 7]==6|7|nil}true


%Built-in functions for lists

 OUTPUT 
{Browse X1}[6 7]从上面来的
{Browse X1.1}6%Head / car
{Browse X1.2}[7]%Tail / cdr
{Browse X2.1}7 


%Recursive function on  a list
%Sum of elements

declare
fun {Sum L}
   if L==nil then 0
   else L.1+{Sum L.2} end
end

{Browse {Sum X1}}



%Tail-recursive Sum
declare
fun {Sum2 L A}
   if L==nil then A
   else {Sum2 L.2 L.1+A} end
end

{Browse {Sum2 X1 0}}


%Nth element of a list

declare
fun {Nth L N}
   if N==1 then L.1
   else {Nth L.2 N-1} end
end


Pattern matching



这个视频讲了case of then else end,难道之前所有练习都可以用if then else end,只填三个空来实现么?那我有好几个练习写多了。。


3. Why list functions are tail recursive


List functions and the kernel language

The kernel language

  • As we mentioned in lesson 1, the kernel language is the simple core language of a programming paradigm
    • We have now seen enough concepts to introduce the kernel language of the functional paradigm
  • All programs in the functional paradigm can be translated into the kernel language
    • All intermediate results of calculations are visible with identifiers (Kernel principle)
    • All functions become procedures with one extra argument
    • Nested function calls are unnested by introducing new identifiers
  • The kernel language is the first part of the formal semantics of a programming language
    • The second part is the abstract machine seen in lesson 6



declare
fun {Append L1 L2}
   case L1
   of nil then L2
   [] H|T then H|{Append T L2}
   end
end

{Browse {Append [1 2 5][8 7 6]}}


declare
proc {Append L1 L2 L3}
   case L1 of nil then L3=L2
   else case L1 of H|T then
		   local T3 in
			  L3=H|T3
			  {Append T L2 T3}
		   end
		end
   end
end

local R in {Append [1 2 5] [8 7 6] R}
   {Browse R}
end

下面这个又是一种新的print的方式。


这周有一个不算分的 bonus challenge,老师的textbook上面有道差不多的,不算分的bonus难道是多做一道题= =

这周的练习我仍然是用增长版的if then else end写的,不知道什么时候才能写得简洁,然后用用其他语法。。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值