wangjicheng886的个人空间 https://blog.eetop.cn/1774723 [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

skill language user guide 36--38

热度 10已有 277 次阅读| 2023-5-25 19:59 |系统分类:芯片设计

Building Lists

There are several main ways to build a list.

1)Specify all the elements of the list literally with the single quote (') operator.

使用单引号(')操作符以字面方式指定列表的所有元素。

2)Specify all the elements as evaluated arguments to the list function.

将所有元素作为待求值参数指定给列表函数。

3)Add an element to an existing list with the cons function.

4)Merge two lists with the append function.


Specify   /ˈspesɪfaɪ/  vt 具体说明;明确规定;详述;详列

element  /ˈelɪmənt/  n  元素

literally  /ˈlɪtərəli/  adv  按字面;字面上

evaluate /ɪˈvæljueɪt/  vt  估计;评价;评估  


Both the cons and append functions allocate a new list and return it to you. You shouldstore the return result in a variable. Otherwise, you cannot refer to the list later.

cons函数和append函数都会新分配一个列表并将其返回给你。你应该将返回结果存储在一个变量中,否则你就无法后续引用这个列表。

The following functions allow you to construct new lists and work with existing lists in different ways.

下面这些函数可以用来构建新列表以及以不同的方式操作已存在的列表。


allocate  /ˈæləkeɪt/  vt 分配;拨…(给);划…(归)

store   vt 贮存,贮藏;装满;供应,供给


Making a list from given elements


The single quote (') operator builds a list using the arguments exactly as they are presented. The values of a and b are irrelevant. The list function fetches the values of variables for inclusion in a list.

单引号(')运算符使用参数构建列表,这些参数会按照它们的顺序被加入到列表中。

变量a和b的值是无关紧要的。而list函数则会获取变量的值,并将其放入新建的列表中。

'(1 2 3) ==> (1 2 3)

a=1 b=2

list(a b 3) ==>(1 2 3)


exactly   /ɪɡˈzæktli/   adv 确切地;准确地;精确地;

irrelevant   /ɪˈreləvənt/  adj  无关的;无关紧要的;不相关的

fetch  /fetʃ/  vt 取来;(去)拿来;(去)请来;


Adding an element to the front of a list (cons)


You should store the return result from cons in a variable. Otherwise, you cannot refer to the list later. Commonly, you store the result back into the variable containing the target list.

你应当将cons函数返回的结果存储到一个变量中,否则,你将无法在以后引用该列表。

通常情况下,你需要将结果存回到原本包含目标列表的那个变量中。

result='(1 2)

result=cons(1 result)   ;将结果存回到原本包含目标列表的那个变量中

==>(1 1 2)



target  /ˈtɑːɡɪt/  目标;指标;(攻击的)目标


Merging two lists (append)


You should store the return result from append in a variable. Otherwise, you cannot refer to the list later.

你应该将append函数返回的结果存储到一个变量中,否则,你将无法在之后引用这个列表。

oneList='(4 5 6)

aList='(1 2 3)

bList=append(aList oneList)

==>(1 2 3 4 5 6)


Common Questions and Answers

People often feel that nil, and the cons and append functions are difficult to understand. Let’s look at some typical questions.

人们常常觉得nil、cons和append函数很难理解。让我们来看看一些典型问题。

Typical Questions                                       

1)What’s the difference between  nil and '( nil )?                                 

nil is a list containing nothing. Its length is 0.   '( nil ) builds a list containing a single element. The length is 1.


2)How can I add an element to the   end of a list?                                   

Use the list function to build a list containing the individual elements. Use the append function to merge it to the first list. More efficient ways to add an element to the end of a list are discussed in a later chapter.

使用list函数构建一个包含个别元素的列表。使用append函数将其合并到第一个列表中。关于如何更有效地向列表末尾添加一个元素,稍后的章节中会进行探讨。

3)Can I reverse the order of the arguments to the cons function? Will the results be the same?

    我可以翻转cons函数的参数顺序吗?结果会相同吗?

You might think that simply reversing the elements to the cons function will put the element on the end of the list. However, this is not the case.  你可能认为只需简单地将元素的顺序反转传递给cons函数就可以将该元素放在列表的末尾。然而,情况并非如此。


可以使用cons函数翻转参数的顺序,但会得到不同的结果。如果将第一个参数和第二个参数反过来,返回的就是不同的列表。


individual  /ˌɪndɪˈvɪdʒuəl/  n  个人;与众不同的人;有个性的人;

                                         adj  个别的;单独的;一个人的;

efficient   /ɪˈfɪʃnt/  adj  有效率的;效率高的;有能力的

reverse   /rɪˈvɜːs/  v  颠倒;彻底转变;使完全相反;


4)What’s the difference between cons and append?

The cons function requires only that its second argument be a list. The length of the resulting list is one more than the length of the original list. The append function requires that both its arguments be lists. The length of the resulting list is the sum of the lengths of the two argument lists. append is slower than cons for large lists.

cons函数只要求其第二个参数是一个列表。结果列表的长度比原来的列表长度多1。

而append函数要求其两个参数都是列表。结果列表的长度是两个参数列表长度的总和。

对于大型列表,append函数比cons函数慢。

a='(1 2)

b='(3 4)

cons(a b) ==>((1 2) 3 4)  

append(a b) ==>(1 2 3 4)   ;append完全将“a b"变量的列表参数,糅合到一个列表里。


original  adj  起初的;原来的;最早的;首创的;独创的;





点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 0

    关注
  • 28

    粉丝
  • 7

    好友
  • 19

    获赞
  • 8

    评论
  • 131

    访问数

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-6-20 16:57 , Processed in 0.043773 second(s), 7 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部