首页 > 其他分享 >CS_61A_lab10

CS_61A_lab10

时间:2023-04-17 19:13:56浏览次数:35  
标签:old car list pair cdr lab10 CS new 61A

Write sub-all, which takes a list s, a list of old words, and a list of new words; the last two lists must be the same length. It returns a list with the elements of s, but with each word that occurs in the second argument replaced by the corresponding word of the third argument. You may use substitute in your solution. Assume that olds and news have no elements in common.

eg:

(sub-all '(go ((bears))) '(go bears) '(big game))

(big ((game)))

 

(define (substitute s old new)
(cond
  ((null? s) nil)
  ((pair? s)
     (cons (substitute (car s) old new)
           (substitute (cdr s) old new)))
  ((equal? old s) new)
  (else s)
)
)
;很聪明,就是把所有的问题转化为对第一个的处理,也就是最基础的情况,而上面的pair就是处理是list的情况
(define (sub-all a olds news)
(if (null? olds)
    a
    (sub-all (substitute a (car olds) (car news))
             (cdr olds) (cdr news))))

 

而我的代码一坨狗屎:

(define (substitute s old new)
  (if (null? s)
      nil
      (if (pair? (car s))
                 (cons (substitute (car s) old new) (substitute (cdr s) old new))
          (if (list? (car s))

            (if (equal? (list old) (car s))
                (cons (list new) (substitute (cdr s) old new))
                (cons (list (car s)) (substitute (cdr s) old new))
            )
            (if (equal? old (car s))
                (cons new (substitute (cdr s) old new))
                (cons (car s) (substitute (cdr s) old new))
            )
        )
    )
)
)

重点在于,我没有认识到 [pair? ] and [list?]其实起到了相同的作用 ,都是判断是否存在括号。

在 Scheme 中,pairlist 都是数据类型,用于组合多个值以形成复合数据结构。然而,它们有一些区别,包括以下几点:

  1. pair 表示任何有两个元素的序列。通常,第一个元素是该对的“car”(参考自cons单元),第二个元素是该对的“cdr”(参考自cons单元)。

    例如,(cons 1 2) 创建了一个新的 pair,它的 car 是 1,cdr 是 2。(cons 'a 'b) 创建了一个新的 pair,它的 car 是 'a,cdr 是 'b。

  2. list 是一种特殊类型的 pair 序列,其中最后一个 pair 的 cdr 是空列表 ()

    例如,(list 1 2 3) 创建了一个新的 list,包含元素 1、2 和 3。(list 'a 'b) 创建了一个新的 list,包含元素 'a 和 'b。(cons 1 (cons 2 (cons 3 '()))) 也是一个 list,它包含元素 1、2 和 3。

  3. pairlist 在其长度和结构上有所不同。 pair 可以包含任何两个元素,而 list 仅包含零个或多个元素,并且最后一个元素的 cdr 始终是空列表 ()

总的来说,pair 通常用于表示数据结构中的节点,例如二叉树和图等,而 list 通常用于表示有序的数据集合。在 Scheme 中,pairlist 通常可以相互转换,因为任何一个合法的 list 都是一个由 pair 组成的链表,而任何一个合法的 pair 序列都可以被视为一个 list

 

标签:old,car,list,pair,cdr,lab10,CS,new,61A
From: https://www.cnblogs.com/xuenima/p/17326830.html

相关文章

  • zxscs
    //zjscs.cpp--minimumcostspanningtree#include<limits.h>#include<stdio.h>#include<stdlib.h>#include"myconst.h"#defineMAX_VERTEX_NUM20#defineINFINITYINT_MAXtypedefintVRType;typedefintVertexType;typedefs......
  • css实现艺术字
    最近在写一个新的小活动,本来有涉及艺术字的标题,本来是想用图片展示的,后来面向百度编程的时候,发现可以使用css来实现效果及代码如下font-size:60rpx;color:#fff;text-shadow:005px#FF0200,0010px#727272;text-align:center;margin:30rpxau......
  • ECShop开源商城与COS互通:降低本地存储负载、提升访问体验
    ECShop简介ECShop是一款开源电子商务平台,具有简单易用、安全稳定、模块化设计等特点。它提供了完整的电子商务解决方案,包括商品管理、订单管理、支付管理、配送管理、会员管理、促销管理、数据统计等功能。ECShop支持多语言、多货币、多种支付方式和配送方式,并可通过插件扩展更多......
  • csdn跳过『关注博主即可阅读全文』
    垃圾CSDN搞出来一个『关注博主即可阅读全文』的功能,非常的污染关注列表  以下脚本可以跳过关注阅读全文:1vararticle_content=document.getElementById("article_content");2article_content.removeAttribute("style");34varfollow_text=document.getElementsByCl......
  • 什么是本地化OCSP?
    证书提供中国境内的OCSP缓存服务器,有助于HTTPS服务器访问的稳定性和可用性。什么是OCSP?OCSP(OnlineCertificateStatusProtocol)即在线证书状态协议,是一个互联网协议,用于获取符合X.509标准的数字证书的状态,是维护服务器和其它网络资源安全的两种普通方法之一。一般浏览器在打开......
  • CSS3 Media Query CSS3媒介查询
    通过min-width的设置,我们可以在浏览器窗口或设备屏幕宽度高于这个值的情况下,为页面指定一个特定的样式表;max-width则反之如下面代码:<style> /*max-width宽度小于600px*/ @mediascreenand(max-width:600px){ .one{background:#F9C;} span.lt600{display:inline......
  • Ez Forensics详解
    EzForensics详解题目要求:数据库版本+字符集格式+最长列名示例:NSSCTF步骤:解压压缩包得到forensics.vmdk,.vmdk是虚拟机磁盘文件的元数据文件可以用美亚的取证大师直接导入自动分析,也可以使用diskgenius挂载有四个压缩包,其中3.zip被删除了在丢失文件中可以找到也......
  • CS229
    IntroductionaboutMLdefinitionsArthurSamuel(1959):fieldofstudythatgivescomputerstheabilitytolearnwithoutbeingexplicitlyprogrammedTomMitchell(1998):AcomputerprogramissaidtobelearnfromexperienceEwithrespecttosometaskTa......
  • CS230
    IntroductionandBasisIntroductionwhydeeplearningtakingoff:dataandtheactionofcollectingdataincreasingML'sperformancebecomesplateuswhentrainingdatasetbecomingbigger,butDLkeepperformingbetterandbettersomeAIcategorie......
  • Element Plus错误警告 | Popper: Detected CSS transitions on at least one of the f
    这个错误的解决方案如下:1.禁用"computeStyles"修饰符的adaptive选项:这将允许平滑过渡,但可能会降低性能。禁用"computeStyles"修饰符的adaptive选项,可以在创建Popper实例时指定modifiers参数,并将computeStyles的adaptive属性设置为false。示例如下:import{create......