The little schemer learn

Reference article:

The Little Schemer Reading Notes-CSDN Blog

Preface

Atom is one of the basic elements of Scheme. First, a procedure is defined atom?to determine whether an S-expression is atomic:

(define atom?

(lambda (x)

(and (not (pair? x)) (not (null? x)))))

This "pair" is actually a list.

However, it should be noted that pair and list are two different things. A pair is expressed as '(1 . 2)a dot-separated tuple like this.

Chapter One

S-expressions include ① atoms; ② lists.

car takes the first S-expression of a non-empty list .

cdr takes a list(car list) consisting of all remaining elements  except the non-empty list list .

cons adds an S-expression to the beginning of a list.

null?  Determine whether the S-expression is an empty list.

eq?  determines whether two S-expressions are the same.

The default parsing method of list is: use car as the function name and cdr as the parameter list to call the function. The evaluated result of the entire list is the return value of the function. When certain "keywords" are used as car, the evaluation rules will change. This requires detailed analysis of specific issues. This question can be referred to Exercise 1.6 of SICP. (There are few "keywords" and it's not complicated)

quote  or  '  are used to suppress the evaluation of S-expressions. Since S-expressions are recursive structures, individual subexpressions of a suppressed S-expression are not evaluated. The quoted part is the code as "data". The result of the quoted atom is itself, similar to the enum of C-based languages; the result of the quoted number atom is still a number; the result of the quoted list is an unevaluated list, similar to a structure such as a linked list.

Chapter 2 Process, process, process again and again

This chapter starts from lat?the implementation of functions and discusses the basic ideas and methods of recursive processing of lat.

The definition process lat?is used to determine whether the subexpressions of the list are all atoms, that is, whether the list is lat (list of atoms). Before Chapter 5, the lists involved were basically lat.

(define lat?
  (lambda (list)
    (cond ((null? list) #t)
          ((atom? (car list)) (lat? (cdr list)))
          (else #f)))`

The special form condis lazy, that is, if the predicate of a clause is true, the following clauses are no longer checked.

The definition process member?is used to determine whether an atom is a member of a certain lat. This function is important, especially for Chapter 7 of Implementing Collections.

(define member?
  (lambda (x lat)
    (cond ((null? lat) #f) ;找遍列表也没找到
          ((eq? x (car lat)) #t)
          (else (member? x (cdr lat))))))

third chapter

Just read the reference article, it summarizes it well.

Chapter 9 Lambda Terminator

Guess you like

Origin blog.csdn.net/zaizai1007/article/details/133465815