tailieunhanh - Schaum’s Outline Series OF Principles of Computer Science phần 4

Định nghĩa của một thuật toán Một thuật toán là một phương pháp để giải quyết một lớp vấn đề. Trong khi các nhà khoa học máy tính suy nghĩ rất nhiều về các thuật toán, thời hạn áp dụng đối với bất kỳ phương pháp giải quyết một loại cụ thể của vấn đề. Hướng dẫn sửa chữa cho chiếc xe của bạn sẽ mô tả một thủ tục, cũng có thể | CHAP. 4 SOFTWARE 59 define sum lambda n cond null n 0 null cdr n car n else car n listSum cdr n The code for sum is similar to listSum in its checking for the length of n the set of parameters in the case of sum but if the number of parameters is two or greater sum uses the listSum function to compute the sum of elements 2 through n. That is because listSum expects a list as an argument whereas sum expects one or more separate arguments that get concatenated into a new list. If sum were to recursively call itself passing cdr n the next call to sum would create cdr n a list of one element the element is another list the cdr of the list of parameters and the second line of cond would return a list instead of a number. Our version of sum now behaves like and will accept any number of parameters and return the sum. sum 2 4 5 6 17 A more elegant solution accepts the list builds a new expression by putting the in front of the list and passes the new list to the Scheme eval function directly. The eval function is the function that Scheme itself uses to evaluate expressions. define sum lambda n cond null n 0 else eval cons n This solution introduces two new elements of Scheme syntax. To understand this version you need to know that the single quote before the stops Scheme from evaluating the function and instead forces Scheme to treat as a simple character atom. In addition the function cons creates a new list by adding an element to the front of a list in this case adding the to the front of the list of numbers to be summed. Functional programming has the desirable properties of simple syntax and semantics and compact code. Also since a function may not change any of the parameters passed to it and since assignment is not used to change program state side effects any changes in variables that endure after execution of the code are eliminated with resulting improvements in reliability. Historically functional programming has found advocates in the fields of artificial .

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã 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.