Xác định một danh sách trong Scheme

Giả sử chúng ta muốn tạo một danh sách các ký hiệu có tên in là các từ tiếng Anh cho năm số nguyên đầu tiên. Tất nhiên, chúng ta có thể làm điều đó bằng cách sử dụng trích dẫn, như thế này

Scheme>[define firstfive '[one two three four five]]
#void
Scheme>firstfive
[one two three four five]

Chúng ta không phải trích dẫn từng biểu tượng riêng lẻ. Trong một biểu thức

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
2, mọi thứ được coi là dữ liệu theo nghĩa đen, không phải biểu thức để đánh giá

Chúng tôi cũng có thể làm điều đó bằng cách gọi

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
3 để xây dựng danh sách và trao cho nó từng ký hiệu trong số năm ký hiệu dưới dạng chữ. Để làm được điều đó, chúng ta phải trích dẫn chúng, để Scheme không nghĩ rằng chúng ta đang đề cập đến các biến có tên
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
4,
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
5, v.v.

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
3 là một thủ tục nên các biểu thức đối số của nó được đánh giá. Chúng tôi sử dụng một
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
2 xung quanh mỗi biểu thức, để nó sẽ trả về một con trỏ tới ký hiệu thích hợp, thay vì giá trị của biến cùng tên

Điều này hoạt động cho dù có một biến theo tên đó hay không, bởi vì tên của các ký hiệu và tên của các biến là những thứ hoàn toàn khác nhau

Ví dụ: ngay cả sau khi đánh giá các biểu thức trên, việc cố gắng đánh giá biểu thức

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
8 sẽ là một lỗi, trừ khi chúng tôi đã xác định một biến có tên
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
8. Sự tồn tại của một biểu tượng với tên in nhất định không nói lên điều gì về sự tồn tại của một biến có tên đó

Danh sách không đồng nhất

Vì Scheme được nhập động nên chúng ta có thể đặt bất kỳ loại đối tượng nào trong danh sách. Cho đến giờ, chúng ta đã tạo một danh sách các số nguyên và một danh sách các ký hiệu. Chúng ta cũng có thể tạo một danh sách gồm nhiều loại nội dung khác nhau, chẳng hạn như danh sách các số nguyên, ký hiệu và danh sách

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
0

Ở đây chúng tôi đã tạo một danh sách hỗn hợp có phần tử đầu tiên là một ký hiệu, phần tử thứ hai là một số nguyên, phần tử thứ ba là danh sách các ký hiệu, phần tử thứ tư là một chuỗi và phần tử thứ năm là một số nguyên khác. [Thuật ngữ kỹ thuật cho danh sách hỗn hợp là "danh sách không đồng nhất. "]

Chúng ta có thể vẽ nó như thế này

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
1

Lưu ý rằng chúng tôi vẽ các ký hiệu [

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
4,
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
01,
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
02 và
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
03] dưới dạng các chuỗi ký tự đơn giản. Đây chỉ là một quy ước vẽ. Chúng thực sự là những đối tượng, giống như các cặp. Chúng tôi vẽ các chuỗi tương tự, nhưng với dấu ngoặc kép xung quanh chúng. Đừng để bị lừa--đây cũng là những đối tượng trên đống. Chúng tôi chỉ vẽ chúng theo cách này để giữ cho bức tranh không bị lộn xộn

Thao tác trên danh sách

Chúng ta đã thấy hai quy trình xử lý danh sách mà bạn sẽ sử dụng rất nhiều,

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
04 và
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05.
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
04 lấy một con trỏ tới một cặp và trích xuất giá trị của trường [
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
04] đầu tiên của nó.
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05 lấy một con trỏ tới một cặp và trả về giá trị của trường thứ hai [
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05] của nó

[Sẽ tốt hơn nếu

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
04 được gọi là
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
11 và
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05 được gọi là
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
13, vì điều đó gợi ý nhiều hơn về cách chúng được sử dụng. một con trỏ tới mục đầu tiên trong danh sách và một con trỏ tới cặp đứng đầu phần còn lại của danh sách. ]

Với danh sách của chúng tôi được lưu trữ trong

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
14, chúng tôi có thể trích xuất các phần của danh sách bằng cách sử dụng
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
04 và
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
9

Bằng cách sử dụng

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
04 và
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05 nhiều lần, chúng tôi có thể trích xuất những thứ ngoài phần tử đầu tiên. Ví dụ: lấy
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05 trong số
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05 của danh sách sẽ bỏ qua hai phần tử đầu tiên và trả về phần còn lại

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
4

Lấy xe của danh sách đó [tức là,

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
04 của
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05 của
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05] trả về mục đầu tiên trong danh sách đó

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
8

Chúng ta có thể tiếp tục làm điều này, ví dụ lấy phần tử thứ hai của danh sách con đó bằng cách lấy ô tô của cdr của nó

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
9

Điều này bắt đầu trở nên tẻ nhạt và khó hiểu--có quá nhiều thủ tục lồng nhau thực hiện quá ít ở mỗi bước--vì vậy Scheme cung cấp một số thủ tục thực hiện hai thao tác danh sách cùng một lúc. Hai cái quan trọng nhất là

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
94 và
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
95

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
94 lấy
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
04 của
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05, cung cấp cho bạn mục thứ hai trong danh sách.
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
95 lấy
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05 của
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05, bỏ qua hai cặp đầu tiên trong danh sách và trả về phần còn lại của danh sách

Điều này cho phép chúng tôi làm điều tương tự như chúng tôi đã làm ở trên, ngắn gọn và dễ đọc hơn một chút

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
8

Với một chút luyện tập, không khó để đọc một vài biểu thức lồng nhau như thế này. Trong ví dụ này, lấy

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
95 của
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
14 bỏ qua hai vị trí trong danh sách, cho chúng ta danh sách bắt đầu bằng danh sách con mà chúng ta muốn. Sau đó, lấy
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
04 của danh sách đó sẽ cho chúng tôi chính danh sách phụ ở phía trước danh sách đó và lấy
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
94 của danh sách đó sẽ cho chúng tôi mục thứ hai trong danh sách phụ

Tất nhiên, ngay cả khi Scheme không cung cấp

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
94 và
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05, bạn có thể tự viết chúng dưới dạng
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
04 và
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
7

Đề án thực sự cung cấp các hoạt động danh sách được xác định trước cho tất cả các kết hợp của tối đa bốn

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
04 và
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05. Ví dụ:
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
82 lấy
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
94 của
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
94. [Sơ đồ đặt tên là mẫu của
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
03 và
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
86 phản ánh sự lồng ghép tương đương của các lệnh gọi tới
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
04 và
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05. ]

Bạn có thể sẽ không muốn bận tâm đến hầu hết những thứ đó, vì những cái tên không trực quan lắm. Hai thủ tục đáng để biết là

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
89 và
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
90

Danh sách

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
91 n
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
92 trích phần tử thứ n của danh sách
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
3, tương đương với n-1 ứng dụng của
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05 theo sau bởi
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
04. Ví dụ:
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
96 tương đương với
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
97 và trả về
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
86. ]

Trên thực tế, bạn có thể lập chỉ mục cho một danh sách như thể nó là một mảng, sử dụng

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
89. [Tất nhiên, thời gian truy cập một phần tử của danh sách là tuyến tính trong chỉ mục của phần tử. Nếu bạn cần truy cập thời gian liên tục, bạn có thể sử dụng vectơ, i. e. , mảng một chiều. ] Lưu ý rằng việc đánh số dựa trên số 0, đó là lý do tại sao
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
80 trả về phần tử thứ tư của
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
81. Điều này phù hợp với việc lập chỉ mục các vectơ, cũng dựa trên số 0, cũng như phản ánh số lượng hoạt động của
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05

Danh sách

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
83 n
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
92 bỏ qua n phần tử đầu tiên của danh sách và trả về một con trỏ tới phần còn lại, tương đương với các ứng dụng lặp đi lặp lại của
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05. [Đây là Sơ đồ R4RS tiêu chuẩn, nhưng không phải Sơ đồ IEEE. Nếu Đề án của bạn không cung cấp
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
90, bạn có thể dễ dàng viết. ]

Hai quy trình này có thể giúp bạn hiểu rõ hơn về những gì bạn đang làm khi trích xuất các phần tử từ danh sách lồng nhau. Giả sử rằng chúng ta có một danh sách

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
87, là một danh sách ba lần--một danh sách các danh sách và chúng ta muốn trích xuất phần tử thứ hai của danh sách cấp dưới cùng là phần tử thứ ba của danh sách cấp giữa mà

Chúng ta có thể viết

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
88, nhưng nó khá khó đọc. Nếu chúng ta sử dụng
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
94,
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
70 và
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
71, chúng ta có thể làm cho nó dễ đọc hơn bằng cách sử dụng một lệnh gọi hàm ở mỗi cấp độ cấu trúc.
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
72. Nhưng viết
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
73 vẫn rõ ràng hơn

hoặc [thụt lề]

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
0

Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
89 và
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
90 thuận tiện hơn nhiều so với những thứ như
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
70 khi các chỉ mục trong danh sách thay đổi theo thời gian chạy. Ví dụ: chúng ta có thể sử dụng một biến chỉ mục
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
77 [hoặc một số biểu thức khác trả về một số nguyên] để chọn ra phần tử thứ i của danh sách.
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
78. Viết cái này với
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
04 và
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05 sẽ yêu cầu viết một vòng lặp hoặc đệ quy để thực hiện n-1
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
05 và một
Scheme>[define firstfive [list 'one 'two 'three 'four 'five]]
#void
Scheme>firstfive
[one two three four five]
04

Danh sách trong Đề án là gì?

Ngược lại với các loại dữ liệu phi cấu trúc của Lược đồ, chẳng hạn như ký hiệu và số, danh sách là cấu trúc chứa các giá trị khác dưới dạng phần tử. Danh sách là tập hợp các giá trị được sắp xếp theo thứ tự . Trong Đề án, danh sách có thể không đồng nhất, trong đó chúng có thể chứa các loại giá trị khác nhau.

let có nghĩa là gì trong Đề án?

Trong Scheme, bạn có thể sử dụng các biến cục bộ gần giống như cách bạn làm trong hầu hết các ngôn ngữ. Khi bạn nhập biểu thức let, các biến let sẽ được liên kết và khởi tạo giá trị . Khi bạn thoát khỏi biểu thức let, các ràng buộc đó sẽ biến mất.

Chủ Đề