Learn Prolog Now phần 6

Đó là, danh sách S là một hậu tố của L danh sách nếu có một số danh sách như vậy mà L là kết quả của concatenating rằng danh sách với S. vị này tìm thấy thành công hậu tố của danh sách, và hơn thế nữa, thông qua thụt lùi, tìm thấy tất cả: | 86 Chapters. More Lists suffix S L - append _ S L . That is list S is a suffix of list L if there is some list such that L is the result of concatenating that list with S. This predicate successfully finds suffixes of lists and moreover via backtracking finds them all suffix X a b c d . X a b c d X b c d X c d X d X no Make sure you understand why the results come out in this order. And now it s very easy to define a program that finds sublists of lists. The sublists of a b c d are a b c d a b b c c d d e a b c b c d and a b c d . Now a little thought reveals that the sublists of a list L are simply the prefixes of suffixes of L. Think about it pictorially List lake suffix lake prefix to get sublist And of course we have both the predicates we need to pin this ideas down we simply define sublist SubL L - suffix S L prefix SubL S . That is SubL is a sublist of L if there is some suffix S of L of which SubL is a prefix. This program doesn t explicitly use append but of course under the surface that s what s doing the work for us as both prefix and suffix are defined using append. . Reversing a list 87 Reversing a list Append is a useful predicate and it is important to know how to use it. But it is just as important to know that it can be a source of inefficiency and that you probably don t want to use it all the time. Why is append a source of inefficiency If you think about the way it works you ll notice a weakness append doesn t join two lists in one simple action. Rather it needs to work its way down its first argument until it finds the end of the list and only then can it carry out the concatenation. Now often this causes no problems. For example if we have two lists and we just want to concatenate them it s probably not too bad. Sure Prolog will need to work down the length of the first list but if the list is not too long that s probably not too high a price to pay for the ease of working with append. But matters may be very different if the first two

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.