Có bao nhiêu loại câu lệnh lặp trong python?

[Trang chủ CS111] [Giáo trình] [Ghi chú bài giảng] [Bài tập] [Phòng thí nghiệm] [Chương trình] [Tài liệu] [Cài đặt phần mềm] [Câu hỏi thường gặp] [Phòng CS. ] [CWIS]

Chúng tôi đã nghiên cứu đệ quy cho phép chúng tôi xây dựng các mẫu phức tạp và giải quyết các vấn đề bằng cách sử dụng lặp đi lặp lại cùng một phương pháp. Lặp lại là một cách khác để diễn đạt "làm điều gì đó nhiều lần". Hầu hết các vấn đề có thể được giải quyết thông qua cả đệ quy và lặp lại, nhưng một dạng có thể dễ sử dụng hơn nhiều so với dạng kia. Chúng ta sẽ nghiên cứu ba hình thức lặp. đệ quy đuôi, vòng lặp
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
2 và vòng lặp
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
0

Chúng ta sẽ sử dụng tác vụ đảo ngược một danh sách làm ví dụ để minh họa các dạng lặp khác nhau có liên quan với nhau và với đệ quy như thế nào. Việc triển khai đệ quy của

// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
1 được đưa ra dưới đây

public static IntList reverse [IntList L] {
  if [isEmpty[L]] {
    return empty[];
  } else {
    return postpend[reverse[tail[L]], head[L]];
  }
}

public static IntList postpend [IntList L, int n] {
  if [isEmpty[L]] {
    return prepend[n, empty[]];
  } else {
    return prepend[head[L], postpend[tail[L], n]];
  }
}
Phương thức đệ quy
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
2 ở trên không chỉ gọi chính nó mà còn phải làm gì đó với kết quả mà nó nhận được từ việc gọi chính nó. Câu trả lời từ lệnh gọi đệ quy tới
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
2 được sử dụng làm tham số cho
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
4. Chúng tôi gọi
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
4 là một hoạt động đang chờ xử lý trong đệ quy. Mặt khác, lặp lại không có hoạt động đang chờ xử lý. Mỗi lần đi qua vòng lặp là một bộ tính toán hoàn chỉnh. Để đạt được điều này, các biến bổ sung thường là cần thiết để theo dõi trạng thái của mọi thứ trong quá trình lặp lại và để giữ trạng thái một phần của câu trả lời mà chúng ta đang đặt cùng nhau

Bảng lặp

Một phép lặp bao gồm các biến trạng thái theo dõi trạng thái của từng biến lặp cho mỗi lần đi qua phép lặp. Bảng lặp là một cách để trực quan hóa các trạng thái của phép lặp. Các cột của bảng lặp là các biến trạng thái liên quan đến phép lặp và các hàng là giá trị của các biến trạng thái trong mỗi lần gọi của phép lặp

Để giải quyết vấn đề đảo ngược danh sách lặp đi lặp lại, chúng ta cần một chiến lược đảo ngược khác. Một kỹ thuật thay thế để đảo ngược danh sách là tuân theo chiến lược mà người ta sẽ sử dụng để đảo ngược một chồng thẻ. tạo thành một cọc mới bằng cách lặp đi lặp lại loại bỏ thẻ trên cùng của cọc ban đầu và đặt nó vào cọc mới. Khi không còn quân bài nào trong cọc ban đầu, cọc mới chứa các quân bài theo thứ tự ngược lại với cọc ban đầu. Để thực hiện chiến lược lặp đi lặp lại này, chúng tôi cần theo dõi danh sách mà chúng tôi đang đảo ngược và kết quả mà chúng tôi đang xây dựng. Đưa ra một danh sách

// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
6, bảng lặp cho
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
7 sẽ là

listresult
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
8
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
9
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
10
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
11
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
12
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
13
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
14
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
15
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
9
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
17

đệ quy đuôi

Đệ quy đuôi là một phép lặp bao gồm đệ quy, nhưng không có hoạt động đang chờ xử lý. Mối quan hệ giữa bảng lặp và triển khai đệ quy đuôi như sau
  • việc triển khai đệ quy đuôi cần một phương thức phụ trợ nếu có nhiều biến trạng thái trong bảng lặp hơn là có các tham số trong phương thức chính
  • mỗi trạng thái trong bảng lặp ánh xạ tới một tham số trong phương thức đệ quy đuôi
  • nếu có một phương thức phụ, phương thức chính sẽ gọi phương thức phụ với các biến trạng thái được khởi tạo thành các giá trị trong hàng đầu tiên của bảng lặp
  • trường hợp cơ bản của phương thức đệ quy đuôi xác định khi nào dừng đệ quy. Khi đó, nó trả về giá trị của biến trạng thái đang theo dõi kết quả
  • trường hợp chung của phương thức đệ quy đuôi gọi chính nó với các giá trị tham số mới là các giá trị trong hàng tiếp theo của bảng lặp
Nếu chúng ta được cung cấp triển khai đệ quy đuôi của một phương thức, chúng ta có thể lấy được bảng lặp bằng cách tạo một cột cho mỗi tham số trong phương thức đệ quy đuôi

Đối với việc triển khai đệ quy đuôi của

// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
1, chúng tôi thấy rằng chúng tôi cần một phương thức phụ trợ [mà chúng tôi gọi là
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
19] vì
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
2 có một danh sách tham số nhưng bảng lặp lại có hai biến trạng thái [danh sách và kết quả]. Việc thực hiện với ý kiến ​​​​được đưa ra dưới đây

// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}

public static IntList reverse [IntList L] {
  if [isEmpty[L]] {
    return empty[];
  } else {
    return postpend[reverse[tail[L]], head[L]];
  }
}

public static IntList postpend [IntList L, int n] {
  if [isEmpty[L]] {
    return prepend[n, empty[]];
  } else {
    return prepend[head[L], postpend[tail[L], n]];
  }
}
31 vòng lặp

Vòng lặp
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
2 là một dạng lặp không liên quan đến đệ quy. Thay vào đó, cú pháp tạo ra một vòng lặp là một đoạn mã được lặp đi lặp lại nhiều lần trong khi một số điều kiện là đúng. Vòng lặp kết thúc khi điều kiện được chỉ định là sai. Cú pháp chính thức cho vòng lặp Java
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
2 là
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
1
Mối quan hệ giữa vòng lặp
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
2 và bảng lặp và triển khai đệ quy đuôi như sau
  • một vòng lặp
    // main method
    public static IntList reverse[IntList list] {
      // returns the result of the auxiliary method
      // the parameters of the auxiliary method are the
      // values in the first column of the iteration table
      return reverseTail[list, empty[]];
    }
    
    // tail-recursive auxiliary method
    // notice there is one parameter for each state variable
    // [column] of the iteration table
    public static IntList reverseTail[IntList list, IntList result] {
      if [isEmpty[list]] { // base case: when to stop
        return result;     // this is the state variable which holds the answer
      } else {             // general case: do a recursive call
        // notice there are no pending operations!
        // the next value of list is tail[list]
        // the next value of result is prepend[head[list], result]
        return reverseTail[tail[list], prepend[head[list], result]];
      }
    }
    
    2 liên quan đến một điều kiện tiếp tục. Điều này ngược lại với điều kiện dừng, là trường hợp cơ bản của việc triển khai đệ quy đuôi hoặc đệ quy
  • triển khai vòng lặp
    // main method
    public static IntList reverse[IntList list] {
      // returns the result of the auxiliary method
      // the parameters of the auxiliary method are the
      // values in the first column of the iteration table
      return reverseTail[list, empty[]];
    }
    
    // tail-recursive auxiliary method
    // notice there is one parameter for each state variable
    // [column] of the iteration table
    public static IntList reverseTail[IntList list, IntList result] {
      if [isEmpty[list]] { // base case: when to stop
        return result;     // this is the state variable which holds the answer
      } else {             // general case: do a recursive call
        // notice there are no pending operations!
        // the next value of list is tail[list]
        // the next value of result is prepend[head[list], result]
        return reverseTail[tail[list], prepend[head[list], result]];
      }
    }
    
    2 không liên quan đến phương thức phụ trợ. Thay vào đó, tất cả các biến trạng thái trong bảng lặp không phải là tham số của phương thức đều được khởi tạo ở đầu phương thức
  • trong phần thân của vòng lặp, các biến trạng thái được cập nhật thành các giá trị tiếp theo của chúng trong hàng tiếp theo của bảng lặp. Hãy cẩn thận. Thứ tự các biến trạng thái được cập nhật rất quan trọng vì một khi giá trị của biến trạng thái đã được thay đổi, giá trị cũ của nó không thể được sử dụng
  • kết quả thường được trả về sau khi vòng lặp kết thúc
Nếu chúng ta được cung cấp triển khai vòng lặp
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
2 của một phương thức, chúng ta có thể lấy bảng lặp bằng cách tạo một cột cho từng tham số trong phương thức và cho từng biến trạng thái được khởi tạo bên ngoài vòng lặp và được cập nhật bên trong vòng lặp. Các biến được khởi tạo bên ngoài vòng lặp và không được sửa đổi trong vòng lặp thì không cần thiết trong bảng lặp

Đối với việc thực hiện vòng lặp

// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
2 của
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
1, chúng ta thấy rằng chúng ta cần khởi tạo một biến trạng thái result ở đầu phương thức vì
public static IntList reverse [IntList L] {
  if [isEmpty[L]] {
    return empty[];
  } else {
    return postpend[reverse[tail[L]], head[L]];
  }
}

public static IntList postpend [IntList L, int n] {
  if [isEmpty[L]] {
    return prepend[n, empty[]];
  } else {
    return prepend[head[L], postpend[tail[L], n]];
  }
}
60 có một danh sách tham số nhưng bảng lặp có hai biến trạng thái [danh sách và kết quả]. Việc thực hiện với ý kiến ​​​​được đưa ra dưới đây

public static IntList reverse [IntList L] {
  if [isEmpty[L]] {
    return empty[];
  } else {
    return postpend[reverse[tail[L]], head[L]];
  }
}

public static IntList postpend [IntList L, int n] {
  if [isEmpty[L]] {
    return prepend[n, empty[]];
  } else {
    return prepend[head[L], postpend[tail[L], n]];
  }
}
3

public static IntList reverse [IntList L] {
  if [isEmpty[L]] {
    return empty[];
  } else {
    return postpend[reverse[tail[L]], head[L]];
  }
}

public static IntList postpend [IntList L, int n] {
  if [isEmpty[L]] {
    return prepend[n, empty[]];
  } else {
    return prepend[head[L], postpend[tail[L], n]];
  }
}
61 vòng lặp

Vòng lặp
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
0 là một dạng lặp chỉ là vòng lặp
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
2 được viết theo cú pháp nhỏ gọn hơn. Trong khoa học máy tính, chúng tôi gọi hiện tượng này là đường cú pháp. Vì vậy, vòng lặp
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
0 là đường cú pháp cho vòng lặp
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
2. Mỗi vòng lặp
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
0 có thể được viết dưới dạng vòng lặp
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
2 và ngược lại, nhưng không phải lúc nào cũng dễ dàng thực hiện điều đó. Vòng lặp
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
0 thường được sử dụng với mảng mà chúng ta sẽ nghiên cứu trong thời gian tới. Cú pháp của chúng rất hữu ích trong các tình huống trong đó có một bộ đếm được tăng hoặc giảm với mỗi lần đi qua vòng lặp. Cú pháp chính thức cho vòng lặp Java
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
0 là
public static IntList reverse [IntList L] {
  if [isEmpty[L]] {
    return empty[];
  } else {
    return postpend[reverse[tail[L]], head[L]];
  }
}

public static IntList postpend [IntList L, int n] {
  if [isEmpty[L]] {
    return prepend[n, empty[]];
  } else {
    return prepend[head[L], postpend[tail[L], n]];
  }
}
6
Mối quan hệ giữa việc thực hiện vòng lặp
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
0 và vòng lặp
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
2 như sau
  • một vòng lặp Java
    // main method
    public static IntList reverse[IntList list] {
      // returns the result of the auxiliary method
      // the parameters of the auxiliary method are the
      // values in the first column of the iteration table
      return reverseTail[list, empty[]];
    }
    
    // tail-recursive auxiliary method
    // notice there is one parameter for each state variable
    // [column] of the iteration table
    public static IntList reverseTail[IntList list, IntList result] {
      if [isEmpty[list]] { // base case: when to stop
        return result;     // this is the state variable which holds the answer
      } else {             // general case: do a recursive call
        // notice there are no pending operations!
        // the next value of list is tail[list]
        // the next value of result is prepend[head[list], result]
        return reverseTail[tail[list], prepend[head[list], result]];
      }
    }
    
    0 tương đương với vòng lặp Java
    // main method
    public static IntList reverse[IntList list] {
      // returns the result of the auxiliary method
      // the parameters of the auxiliary method are the
      // values in the first column of the iteration table
      return reverseTail[list, empty[]];
    }
    
    // tail-recursive auxiliary method
    // notice there is one parameter for each state variable
    // [column] of the iteration table
    public static IntList reverseTail[IntList list, IntList result] {
      if [isEmpty[list]] { // base case: when to stop
        return result;     // this is the state variable which holds the answer
      } else {             // general case: do a recursive call
        // notice there are no pending operations!
        // the next value of list is tail[list]
        // the next value of result is prepend[head[list], result]
        return reverseTail[tail[list], prepend[head[list], result]];
      }
    }
    
    2 sau đây
    // main method
    public static IntList reverse[IntList list] {
      // returns the result of the auxiliary method
      // the parameters of the auxiliary method are the
      // values in the first column of the iteration table
      return reverseTail[list, empty[]];
    }
    
    // tail-recursive auxiliary method
    // notice there is one parameter for each state variable
    // [column] of the iteration table
    public static IntList reverseTail[IntList list, IntList result] {
      if [isEmpty[list]] { // base case: when to stop
        return result;     // this is the state variable which holds the answer
      } else {             // general case: do a recursive call
        // notice there are no pending operations!
        // the next value of list is tail[list]
        // the next value of result is prepend[head[list], result]
        return reverseTail[tail[list], prepend[head[list], result]];
      }
    }
    
    7
  • một lần nữa, một vòng lặp
    // main method
    public static IntList reverse[IntList list] {
      // returns the result of the auxiliary method
      // the parameters of the auxiliary method are the
      // values in the first column of the iteration table
      return reverseTail[list, empty[]];
    }
    
    // tail-recursive auxiliary method
    // notice there is one parameter for each state variable
    // [column] of the iteration table
    public static IntList reverseTail[IntList list, IntList result] {
      if [isEmpty[list]] { // base case: when to stop
        return result;     // this is the state variable which holds the answer
      } else {             // general case: do a recursive call
        // notice there are no pending operations!
        // the next value of list is tail[list]
        // the next value of result is prepend[head[list], result]
        return reverseTail[tail[list], prepend[head[list], result]];
      }
    }
    
    0 liên quan đến một điều kiện tiếp tục. Điều này ngược lại với điều kiện dừng, là trường hợp cơ bản của việc triển khai đệ quy đuôi hoặc đệ quy. Điều kiện tiếp tục của các vòng lặp
    // main method
    public static IntList reverse[IntList list] {
      // returns the result of the auxiliary method
      // the parameters of the auxiliary method are the
      // values in the first column of the iteration table
      return reverseTail[list, empty[]];
    }
    
    // tail-recursive auxiliary method
    // notice there is one parameter for each state variable
    // [column] of the iteration table
    public static IntList reverseTail[IntList list, IntList result] {
      if [isEmpty[list]] { // base case: when to stop
        return result;     // this is the state variable which holds the answer
      } else {             // general case: do a recursive call
        // notice there are no pending operations!
        // the next value of list is tail[list]
        // the next value of result is prepend[head[list], result]
        return reverseTail[tail[list], prepend[head[list], result]];
      }
    }
    
    0 và
    // main method
    public static IntList reverse[IntList list] {
      // returns the result of the auxiliary method
      // the parameters of the auxiliary method are the
      // values in the first column of the iteration table
      return reverseTail[list, empty[]];
    }
    
    // tail-recursive auxiliary method
    // notice there is one parameter for each state variable
    // [column] of the iteration table
    public static IntList reverseTail[IntList list, IntList result] {
      if [isEmpty[list]] { // base case: when to stop
        return result;     // this is the state variable which holds the answer
      } else {             // general case: do a recursive call
        // notice there are no pending operations!
        // the next value of list is tail[list]
        // the next value of result is prepend[head[list], result]
        return reverseTail[tail[list], prepend[head[list], result]];
      }
    }
    
    2 có thể hoàn toàn giống nhau
  • nói chung, một biến trạng thái [trong một số trường hợp có thể có nhiều hơn] trong bảng lặp được gắn nhãn là bộ đếm vòng lặp
    // main method
    public static IntList reverse[IntList list] {
      // returns the result of the auxiliary method
      // the parameters of the auxiliary method are the
      // values in the first column of the iteration table
      return reverseTail[list, empty[]];
    }
    
    // tail-recursive auxiliary method
    // notice there is one parameter for each state variable
    // [column] of the iteration table
    public static IntList reverseTail[IntList list, IntList result] {
      if [isEmpty[list]] { // base case: when to stop
        return result;     // this is the state variable which holds the answer
      } else {             // general case: do a recursive call
        // notice there are no pending operations!
        // the next value of list is tail[list]
        // the next value of result is prepend[head[list], result]
        return reverseTail[tail[list], prepend[head[list], result]];
      }
    }
    
    0. Nếu bộ đếm không phải là tham số của phương thức, nó sẽ được khởi tạo trong phần khởi tạo bộ đếm của vòng lặp
    // main method
    public static IntList reverse[IntList list] {
      // returns the result of the auxiliary method
      // the parameters of the auxiliary method are the
      // values in the first column of the iteration table
      return reverseTail[list, empty[]];
    }
    
    // tail-recursive auxiliary method
    // notice there is one parameter for each state variable
    // [column] of the iteration table
    public static IntList reverseTail[IntList list, IntList result] {
      if [isEmpty[list]] { // base case: when to stop
        return result;     // this is the state variable which holds the answer
      } else {             // general case: do a recursive call
        // notice there are no pending operations!
        // the next value of list is tail[list]
        // the next value of result is prepend[head[list], result]
        return reverseTail[tail[list], prepend[head[list], result]];
      }
    }
    
    0 thay vì ở đầu phương thức
  • bộ đếm được cập nhật trong phần cập nhật bộ đếm của vòng lặp
    // main method
    public static IntList reverse[IntList list] {
      // returns the result of the auxiliary method
      // the parameters of the auxiliary method are the
      // values in the first column of the iteration table
      return reverseTail[list, empty[]];
    }
    
    // tail-recursive auxiliary method
    // notice there is one parameter for each state variable
    // [column] of the iteration table
    public static IntList reverseTail[IntList list, IntList result] {
      if [isEmpty[list]] { // base case: when to stop
        return result;     // this is the state variable which holds the answer
      } else {             // general case: do a recursive call
        // notice there are no pending operations!
        // the next value of list is tail[list]
        // the next value of result is prepend[head[list], result]
        return reverseTail[tail[list], prepend[head[list], result]];
      }
    }
    
    0 thay vì ở cuối thân vòng lặp
  • Các biến trạng thái không phải là tham số của phương thức cũng không phải là bộ đếm của vòng lặp
    // main method
    public static IntList reverse[IntList list] {
      // returns the result of the auxiliary method
      // the parameters of the auxiliary method are the
      // values in the first column of the iteration table
      return reverseTail[list, empty[]];
    }
    
    // tail-recursive auxiliary method
    // notice there is one parameter for each state variable
    // [column] of the iteration table
    public static IntList reverseTail[IntList list, IntList result] {
      if [isEmpty[list]] { // base case: when to stop
        return result;     // this is the state variable which holds the answer
      } else {             // general case: do a recursive call
        // notice there are no pending operations!
        // the next value of list is tail[list]
        // the next value of result is prepend[head[list], result]
        return reverseTail[tail[list], prepend[head[list], result]];
      }
    }
    
    0 được khởi tạo ở đầu phương thức
  • trong phần thân của vòng lặp, các biến trạng thái được cập nhật thành các giá trị tiếp theo của chúng trong hàng tiếp theo của bảng lặp. Hãy cẩn thận. Thứ tự các biến trạng thái được cập nhật rất quan trọng vì một khi giá trị của biến trạng thái đã được thay đổi, giá trị cũ của nó không thể được sử dụng
  • kết quả thường được trả về sau khi vòng lặp kết thúc
  • có thể để trống một hoặc nhiều thông số kỹ thuật của vòng lặp
    // main method
    public static IntList reverse[IntList list] {
      // returns the result of the auxiliary method
      // the parameters of the auxiliary method are the
      // values in the first column of the iteration table
      return reverseTail[list, empty[]];
    }
    
    // tail-recursive auxiliary method
    // notice there is one parameter for each state variable
    // [column] of the iteration table
    public static IntList reverseTail[IntList list, IntList result] {
      if [isEmpty[list]] { // base case: when to stop
        return result;     // this is the state variable which holds the answer
      } else {             // general case: do a recursive call
        // notice there are no pending operations!
        // the next value of list is tail[list]
        // the next value of result is prepend[head[list], result]
        return reverseTail[tail[list], prepend[head[list], result]];
      }
    }
    
    0. Tuy nhiên, dấu chấm phẩy phải được đặt đúng chỗ để xác định ba phần. Một vòng lặp vô hạn có thể được tạo bằng cú pháp sau
    // main method
    public static IntList reverse[IntList list] {
      // returns the result of the auxiliary method
      // the parameters of the auxiliary method are the
      // values in the first column of the iteration table
      return reverseTail[list, empty[]];
    }
    
    // tail-recursive auxiliary method
    // notice there is one parameter for each state variable
    // [column] of the iteration table
    public static IntList reverseTail[IntList list, IntList result] {
      if [isEmpty[list]] { // base case: when to stop
        return result;     // this is the state variable which holds the answer
      } else {             // general case: do a recursive call
        // notice there are no pending operations!
        // the next value of list is tail[list]
        // the next value of result is prepend[head[list], result]
        return reverseTail[tail[list], prepend[head[list], result]];
      }
    }
    
    5
  • có thể có nhiều câu lệnh trong mỗi phần của thông số kỹ thuật vòng lặp
    // main method
    public static IntList reverse[IntList list] {
      // returns the result of the auxiliary method
      // the parameters of the auxiliary method are the
      // values in the first column of the iteration table
      return reverseTail[list, empty[]];
    }
    
    // tail-recursive auxiliary method
    // notice there is one parameter for each state variable
    // [column] of the iteration table
    public static IntList reverseTail[IntList list, IntList result] {
      if [isEmpty[list]] { // base case: when to stop
        return result;     // this is the state variable which holds the answer
      } else {             // general case: do a recursive call
        // notice there are no pending operations!
        // the next value of list is tail[list]
        // the next value of result is prepend[head[list], result]
        return reverseTail[tail[list], prepend[head[list], result]];
      }
    }
    
    0. Các câu lệnh nên được phân tách bằng dấu phẩy. Ví dụ,
    // main method
    public static IntList reverse[IntList list] {
      // returns the result of the auxiliary method
      // the parameters of the auxiliary method are the
      // values in the first column of the iteration table
      return reverseTail[list, empty[]];
    }
    
    // tail-recursive auxiliary method
    // notice there is one parameter for each state variable
    // [column] of the iteration table
    public static IntList reverseTail[IntList list, IntList result] {
      if [isEmpty[list]] { // base case: when to stop
        return result;     // this is the state variable which holds the answer
      } else {             // general case: do a recursive call
        // notice there are no pending operations!
        // the next value of list is tail[list]
        // the next value of result is prepend[head[list], result]
        return reverseTail[tail[list], prepend[head[list], result]];
      }
    }
    
    7
Nếu chúng ta được cung cấp triển khai vòng lặp
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
0 của một phương thức, chúng ta có thể lấy bảng lặp bằng cách tạo một cột cho từng tham số trong phương thức, cho từng bộ đếm và cho từng biến trạng thái được khởi tạo bên ngoài vòng lặp và được cập nhật bên trong vòng lặp. Các biến được khởi tạo bên ngoài vòng lặp và không được sửa đổi trong vòng lặp thì không cần thiết trong bảng lặp

Đối với việc triển khai vòng lặp

// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
0 của
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
1, việc xác định bộ đếm sẽ hơi phức tạp vì không có bất kỳ số nào tăng hoặc giảm. Thay vào đó, chúng tôi chọn danh sách IntList làm bộ đếm của mình vì danh sách này đang bị thu hẹp sau mỗi lần lặp lại. Vì danh sách là một tham số của phương thức của chúng tôi, nên nó không cần phải khởi tạo. Do đó, sẽ không có gì trong phần khởi tạo ngược của vòng lặp
// main method
public static IntList reverse[IntList list] {
  // returns the result of the auxiliary method
  // the parameters of the auxiliary method are the
  // values in the first column of the iteration table
  return reverseTail[list, empty[]];
}

// tail-recursive auxiliary method
// notice there is one parameter for each state variable
// [column] of the iteration table
public static IntList reverseTail[IntList list, IntList result] {
  if [isEmpty[list]] { // base case: when to stop
    return result;     // this is the state variable which holds the answer
  } else {             // general case: do a recursive call
    // notice there are no pending operations!
    // the next value of list is tail[list]
    // the next value of result is prepend[head[list], result]
    return reverseTail[tail[list], prepend[head[list], result]];
  }
}
0 của chúng tôi. Việc thực hiện với ý kiến ​​​​được đưa ra dưới đây

Có bao nhiêu loại câu lệnh lặp?

Câu lệnh lặp [C++] . Mỗi vòng lặp này lặp lại cho đến khi biểu thức kết thúc của nó ước tính bằng 0 [sai] hoặc cho đến khi kết thúc vòng lặp bị buộc bằng câu lệnh ngắt. four iteration statements — while, do, for, and range-based for. Each of these iterates until its termination expression evaluates to zero [false], or until loop termination is forced with a break statement.

Các câu lệnh lặp khác nhau trong Python là gì?

Các câu lệnh lặp hoặc câu lệnh lặp cho phép chúng ta thực hiện một khối câu lệnh miễn là điều kiện là đúng. Các câu lệnh vòng lặp được sử dụng khi chúng ta cần chạy đi chạy lại cùng một đoạn mã, mỗi lần có một giá trị khác nhau .

3 loại cấu trúc lặp chính là gì?

Điều khiển lặp lại. Câu lệnh LOOP và EXIT. Các câu lệnh LOOP cho phép bạn thực hiện một chuỗi các câu lệnh nhiều lần. Có ba dạng câu lệnh LOOP. LOOP , WHILE-LOOP và FOR-LOOP .

Tuyên bố kích thích trong Python là gì?

Trong Python, câu lệnh lặp còn được gọi là câu lệnh lặp hoặc câu lệnh lặp. Các câu lệnh lặp được sử dụng để thực hiện lặp đi lặp lại một phần của chương trình miễn là một điều kiện nhất định là Đúng . Python cung cấp các câu lệnh lặp sau. câu lệnh while. cho tuyên bố.

Chủ Đề