首页 > 其他分享 >sicp每日一题[2.78]

sicp每日一题[2.78]

时间:2024-11-15 19:19:27浏览次数:1  
标签:2.78 每日 datum sicp number tag scheme type define

Exercise2.78

The internal procedures in the scheme-number package are essentially nothing more than calls to the primitive procedures +, -, etc. It was not possible to use the primitives of the language directly because our type-tag system requires that each data object have a type attached to it. In fact, however, all Lisp implementations do have a type system, which they use internally. Primitive predicates such as symbol? and number? determine whether data objects have particular types. Modify the definitions of type-tag, contents, and attach-tag from Section 2.4.2 so that our generic system takes advantage of Scheme’s internal type system. That is to say, the system should work as before except that ordinary numbers should be represented simply as Scheme numbers rather than as pairs whose car is the symbol scheme-number.


这道题难度不大,就是说我们在设计程序的时候,先用内置的 number? 函数来判断,如果确实是数字,那就不用添加 scheme-number 标签,否则还跟之前一样。

(define (attach-tag type-tag contents)
  (if (number? contents)
      contents
      (cons type-tag contents)))

(define (type-tag datum)
  (cond ((number? datum) 'scheme-number)
        ((pair? datum) (car datum))
        (else (error "Bad tagged datum: TYPE-TAG" datum))))

(define (contents datum)
  (cond ((number? datum) datum)
        ((pair? datum) (cdr datum))
        (else (error "Bad tagged datum: CONTENTS" datum))))

上网看了别人的答案后,我发现 install-scheme-number-package 也能做一定简化,如下所示:

(define (install-scheme-number-package)
  (put 'add '(scheme-number scheme-number) +)
  (put 'sub '(scheme-number scheme-number) -)
  (put 'mul '(scheme-number scheme-number) *)
  (put 'div '(scheme-number scheme-number) /)
  (put 'make 'scheme-number (lambda (x) (attach-tag 'scheme-number x)))
  'done)

(define (make-scheme-number n)
  ((get 'make 'scheme-number) n))

(install-scheme-number-package)

(define m (make-scheme-number 5))
(define n (make-scheme-number 10))
(add m n)
(sub m n)
(mul m n)
(div m n)

; 执行结果
'done
15
-5
50
1/2

标签:2.78,每日,datum,sicp,number,tag,scheme,type,define
From: https://www.cnblogs.com/think2times/p/18548527

相关文章

  • 【SpringBoot每日学习 - 第二天】SpringApplication 启动类:方法篇一
    SpringApplication类是SpringBoot应用程序的核心类之一,负责启动和初始化整个SpringBoot应用。通过调用SpringApplication.run()方法,SpringBoot会启动嵌入式的Web服务器(如Tomcat)并创建Spring容器。SpringApplication类具有一系列方法和配置项,允许开发者自定......
  • 【SpringBoot每日学习 - 第一天】SpringApplication 启动类:属性篇
    SpringApplication类是SpringBoot应用启动的核心类之一,包含了大量的属性,控制着应用启动的各个方面。这些属性涵盖了从配置环境、应用上下文类型、Banner显示、启动日志、事件监听等多个方面。以下是SpringApplication类中重要属性的详细说明及其用途:静态属性DEFAUL......
  • 每日OJ题_牛客_计算字符串的编辑距离_DP_C++_Java
    目录牛客_计算字符串的编辑距离_DP题目解析C++代码Java代码牛客_计算字符串的编辑距离_DP计算字符串的编辑距离_牛客题霸_牛客网描述:Levenshtein 距离,又称编辑距离,指的是两个字符串之间,由一个转换成另一个所需的最少编辑操作次数。许可的编辑操作包括将一个字符替换......
  • 每日
    includeusingnamespacestd;intmain(){intm,n,num;cin>>m>>n;intarr[999999];ints[999999];for(inti=0;i<m;i++){cin>>arr[i];}for(inti=0;i<n;i++){cin>>num;intst=0;in......
  • 每日小题--买股票的最佳时机
    目录题目  分析解题思路完整代码题目 给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。返......
  • sicp每日一题[2.77]
    Exercise2.77LouisReasonertriestoevaluatetheexpression(magnitudez)wherezistheobjectshowninFigure2.24.Tohissurprise,insteadoftheanswershegetsanerrormessagefromapply-generic,sayingthereisnomethodfortheoperationmagni......
  • 每日一题:https://www.luogu.com.cn/problem/P2249
    includeusingnamespacestd;intmain(){intp,sum;cin>>p>>sum;intarr[p];for(inti=0;i<p;i++){cin>>arr[i];}for(inti=1;i<=sum;i++){intmubiao;intmin=0;intmax=p-1;cin>>mubiao;for(;......
  • 每日一题 :https://www.luogu.com.cn/problem/P2249
    includeusingnamespacestd;intmain(){intp,sum;cin>>p>>sum;intarr[p];for(inti=0;i<p;i++){cin>>arr[i];}for(inti=1;i<=sum;i++){intmubiao;intmin=0;intmax=p-1;cin>>mubiao;for(;;){if(arr[0]mubiao){printf(......
  • 每日总结43
    下载python库缓慢时,可以更改为国内的镜像使用国内镜像源:由于pip默认从PyPI下载,而PyPI的服务器在国外,导致下载速度慢。您可以更换为国内的镜像源来加速下载。以下是几个常用的国内镜像源:清华大学:https://pypi.tuna.tsinghua.edu.cn/simple阿里云:https://mirrors.aliyun.com......
  • 每日打卡 11.13
    includeusingnamespacestd;definemax10voidswap(int*px,int*py);voidbubble(inta[],intn);intmain(){intn,a[max];inti;cout<<"输入n"<<endl;cin>>n;cout<<"输入n个数"<<endl;for(i=0;......