zoukankan      html  css  js  c++  java
  • sicp_2.59-2.60

     1 #lang racket
     2 
     3 ;;;;;;;;;;;;;;;;;;;;2.59
     4 (define (element-of-set? x set)
     5   (cond ((null? set) false)
     6         ((equal? x (car set)) true)
     7         (else (element-of-set? x (cdr set)))))
     8 
     9 (define (adjoin-set x set)
    10   (if (element-of-set? x set)
    11       set
    12       (cons x set)))
    13 
    14 (define (intersection-set set1 set2)
    15   (cond ((or (null? set1) (null? set2)) '())
    16         ((element-of-set? (car set1) set2)
    17          (cons (car set1)
    18                (intersection-set (cdr set1) set2)))
    19         (else (intersection-set (cdr set1) set2))))
    20 
    21 (define (union-set set1 set2)
    22   (cond ((null? set1) set2)
    23         ((null? set2) set1)
    24         ((element-of-set? (car set1) set2)
    25          (union-set (cdr set1) set2))
    26         (else (cons (car set1)
    27                     (union-set (cdr set1) set2)))))
    28 
    29 ;;;;;;;;;;;;;;;;;;;2.60
    30 (define (new-element-of-set? x set)
    31   (cond ((null? set) false)
    32         ((equal? x (car set)) true)
    33         (else (element-of-set? x (cdr set)))))
    34 
    35 (define (new-adjion-set x set)
    36   (cons x set))
    37 
    38 (define (new-union-set set1 set2)
    39   (append set1 set2))
    40 
    41 (define (new-intersection-set set1 set2)
    42  (cond ((or (null? set1) (null? set2)) '())
    43         ((element-of-set? (car set1) set2)
    44          (cons (car set1)
    45                (intersection-set (cdr set1) set2)))
    46         (else (intersection-set (cdr set1) set2))))
    47 
    48 ;;;;;;;;;;;;;;;;;test
    49 (union-set '(1 2 3 4) '(0 3 5 1))
    50 (new-union-set '(1 2 3 2 4) '(3 4 3 2 1 5))
    51 (new-intersection-set '(1 3 3 2 4) '(2 3 2 2 4 5 1))
    52 ;;;;;;;;;;;;;;;;adjion和union分别变为O(1)和O(n)
    53 ;;;;;;;;;;;;;;;;其他不变
    Yosoro
  • 相关阅读:
    Cookie与session
    orm之中介模型
    mysql数据库基础命令——幕布链接
    css——幕布链接
    HTML基础——幕布链接
    js基础——幕布
    三层结构与MVC
    [C++]3-1 得分(Score ACM-ICPC Seoul 2005,UVa1585)
    [C++]环状序列(CircularSequence,ACM/ICPC Seoul 2004,UVa1584)
    [C++]猜数字游戏的提示(Master-Mind Hints,UVa340)
  • 原文地址:https://www.cnblogs.com/tclan126/p/6477017.html
Copyright © 2011-2022 走看看