<commit_before>---
layout: post
title: SICP读书笔记2.3
tags: SICP
hidden: true

---

## 练习2.53

``` scheme
#;1359> (list 'a 'b 'c)
(a b c)
#;1400> (list (list 'george))
((george))
#;1451> (cdr '((x1 x2) (y1 y2)))
((y1 y2))
#;1501> (cadr '((x1 x2) (y1 y2)))
(y1 y2)
#;1559> (pair? (car '(a short list)))
#f
#;1606> (memq 'red '((red shose) (blue socks)))
#f
#;1698> (memq 'red '(red shoes blue socks))
(red shoes blue socks)
```


## 练习2.54

``` scheme
(define (equal? a b)
  (if (and (pair? a) (pair? b))
      (and (equal? (car a) (car b))
           (equal? (cdr a) (cdr b)))
      (eq? a b)))

```

## 练习2.55

对一个表达式取引号其实是 (quote sth) 的语法糖

所以题目中描述的表达式相当于

``` scheme
(car (quote (quote abracadabra)))
```

所以得出结果为quote<commit_msg>ex-2.56<commit_after>---
layout: post
title: SICP读书笔记2.3
tags: SICP
hidden: true

---

## 练习2.53

``` scheme
#;1359> (list 'a 'b 'c)
(a b c)
#;1400> (list (list 'george))
((george))
#;1451> (cdr '((x1 x2) (y1 y2)))
((y1 y2))
#;1501> (cadr '((x1 x2) (y1 y2)))
(y1 y2)
#;1559> (pair? (car '(a short list)))
#f
#;1606> (memq 'red '((red shose) (blue socks)))
#f
#;1698> (memq 'red '(red shoes blue socks))
(red shoes blue socks)
```


## 练习2.54

``` scheme
(define (equal? a b)
  (if (and (pair? a) (pair? b))
      (and (equal? (car a) (car b))
           (equal? (cdr a) (cdr b)))
      (eq? a b)))
```

## 练习2.55

对一个表达式取引号其实是 (quote sth) 的语法糖

所以题目中描述的表达式相当于

``` scheme
(car (quote (quote abracadabra)))
```

所以得出结果为quote

## 练习2.56

### 实现相关函数

``` scheme
(define (exponentiation? x)
  (and (pair? x) (eq? (car x) '**)))

(define (base s) (cadr s))
(define (exponent s) (caddr s))

(define (make-exponentiation base exp)
  (cond ((=number? base 0) 0)
        ((=number? exp 0) 1)
        ((=number? exp 1) base)
        (else (list '** base exp))))
```

### 添加相关代码到`deriv`

``` scheme
(define (deriv exp var)
  (cond ((number? exp) 0)
        ((variable? exp) (if (same-variable? exp var) 1 0)) ((sum? exp) (make-sum (deriv (addend exp) var)
                                                                                  (deriv (augend exp) var)))
        ((product? exp)
         (make-sum
          (make-product (multiplier exp)
                        (deriv (multiplicand exp) var))
          (make-product (deriv (multiplier exp) var)
                        (multiplicand exp))))
        ((exponentiation? exp)
         (make-product (make-product (exponent exp)
                                     (make-exponentiation (base exp) (make-sum (exponent exp) -1)))
                       (deriv (base exp) var)))
        (else
         (error "unknown expression type: DERIV" exp))))
```
{'fraction_non_alphanumeric': 0.20167427701674276, 'fraction_numerical': 0.04452054794520548, 'mean_word_length': 2.4501312335958003, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4327229', 'n_tokens_mistral': 1198, 'n_tokens_neox': 1160, 'n_words': 320}