以下笔记基于Concordia University COMP 348
Determining a subset relation
Consider function issubsetp which takes as arguments two lists representing sets, set1 and set2, and returns true if set1 is a subset of set2. Otherwise, it returns false (nil).
考虑函数issubsetp,该函数将代表set1和set2的两个列表作为参数,如果set1是set2的子集,则返回true。 否则,它返回false(无)。
Base case: If set1 is empty, then return true.
基本情况:如果set1为空,则返回true。
Recursive case: If the first element of set1 is a member of set2, then recur on the rest of the elements of set1, otherwise return false (nil).
递归情况:如果set1的第一个元素是set2的成员,则对set1的其余元素进行递归,否则返回false(无)。
(defun issubsetp (set1 set2)
(if (null set1)
t
(if (member (car set1) set2)
(issubsetp (cdr set1) set2)
nil)))
Determining set union
Consider function setunion which takes as its arguments two lists lst1 and lst2 representing sets and returns the set union.
考虑函数setunion,该函数以两个表示集的列表lst1和lst2作为参数并返回集并集。
Base cases:
– If lst1 is empty, then return lst2. – If lst2 is empty, then return lst1.
如果lst1为空,则返回lst2。 –如果lst2为空,则返回lst1。
Recursive cases:
– If the head of lst1 is a member of lst2, then ignore this element and recur on the tail of lst1, and lst2.
– If the head of lst1 is not a member of lst2, return a list which is the concatenation of this element with the union of the tail of lst1 and lst2.
–如果lst1的头部是lst2的成员,则忽略此元素并在lst1和lst2的尾部重复。
–如果lst1的头部不是lst2的成员,则返回一个列表,该列表是该元素与lst1和lst2的尾部的并集的连接。
(defun setunion (lst1 lst2)
(cond
((null lst1) lst2)
((null lst2) lst1) ((member (car lst1) lst2) (setunion (cdr lst1) lst2))
(t (cons (car lst1) (setunion (cdr lst1) lst2)))))
Determining set intersection
Consider function setintersection which takes as its arguments two lists lst1 and lst2 representing sets, and returns a new list representing a set which forms the intersection of its arguments.
考虑函数setintersection,该函数将两个表示集合的列表lst1和lst2作为其参数,并返回一个表示组成其参数交集的集合的新列表。
Base case: – If either list is empty, then return the empty set.
基本情况:–如果任一列表为空,则返回空集。
Recursive cases:
– If the head of lst1 is a member of lst2, then keep this element and recur on the tail of lst1 and lst2.
– If the head of lst1 is not a member of lst2, ignore this element and recur on the tail of lst1 and lst2.
–如果lst1的头部是lst2的成员,则保留此元素并在lst1和lst2的尾部重复出现。
–如果lst1的头部不是lst2的成员,请忽略此元素,然后在lst1和lst2的末端重复。
Determining set difference
Consider function setdifference which takes as its arguments two lists lst1 and lst2 representing sets and returns the set difference.
考虑函数setdifference,该函数以两个表示集的列表lst1和lst2作为参数并返回集差。
Base case: – If lst1 is empty, then return the empty set. If lst2 is empty, then return lst1.
基本情况:–如果lst1为空,则返回空集。 如果lst2为空,则返回lst1。
Recursive cases:
– If the head of lst1 is a member of lst2, then ignore this element and recur on the tail of lst1, and lst2.
– If the head of lst1 is not a member of lst2, keep this element and recur on the tail of lst1 and lst2.
–如果lst1的头部是lst2的成员,则忽略此元素并在lst1和lst2的尾部重复。
–如果lst1的头部不是lst2的成员,请保留该元素,然后在lst1和lst2的尾部重复。
Determining set symmetric difference
Consider function setsymmetricdifference which takes as its arguments two lists representing sets and returns a list representing their symmetric difference.
考虑函数setsymmetricdifference,该函数将代表集合的两个列表作为参数并返回代表它们的对称差的列表。
We can define this function as the difference between the union and the intersection sets, i.e.
我们可以将此函数定义为并集和相交集之间的差,即
Bags
A bag (or multiset) is a structure which contains a collection of elements.
袋子(或多件套)是一种包含元素集合的结构。
Like a set, the ordering of the elements is not important.
像一组一样,元素的顺序并不重要。
Unlike a set, repetitions are allowed.
与集合不同,允许重复。
Transforming a bag to a set
Consider function bag-to-set which takes as its argument a list representing a bag and returns the corresponding set.
考虑bag-to-set函数,该函数将代表bag的列表作为参数并返回相应的set。
Base case: If the list is empty, then return the empty list.
基本情况:如果列表为空,则返回空列表。
Recursive cases:
– If the head of the list is a member of the tail of the list, then ignore this element and recur on the tail of the list.
– If the head of the list is not a member of the tail of the list, keep the head element and recur on the tail of the list.
–如果列表的开头是列表结尾的成员,则忽略此元素,然后在列表的结尾重复。
–如果列表的开头不是列表结尾的成员,则保留head元素,然后重复出现在列表的结尾。
Ordered structures: Tuples
We have already seen an ordered structure, namely the list.
我们已经看到了有序结构,即列表。
A tuple is a structure which contains a collection of elements.
元组是包含元素集合的结构。
Unlike sets and bags, the ordering of the elements matters.
与套装和袋子不同,元素的顺序很重要。
Unlike a set repetitions are allowed.
与集合不同,允许重复。
Representing trees
Bubble sort
Consider the implementation of function bubble-sort which takes as its argument a list and returns the same list with its elements sorted in an ascending order.
考虑函数bubble-sort的实现,该函数将一个列表作为其参数,并返回相同的列表,其元素以升序排序。
We first need to build some auxiliary functions, the first one is bubble which performs one iteration, thus placing one element in its proper position.
我们首先需要构建一些辅助功能,第一个是气泡,它执行一次迭代,从而将一个元素放置在适当的位置。
Another auxiliary function is is-sortedp which returns True or False on whether or not its list argument is sorted.
另一个辅助函数是is-sortedp,该函数根据其list参数是否已排序返回True或False。
Searching
Searching is a technique to determine whether or not a given element appears in a sorted list of elements.
搜索是一种确定给定元素是否出现在元素排序列表中的技术。
We will deploy a list to perform searching.
我们将部署一个列表来执行搜索。