申请不是程序(方案图程序) [英] Application not a procedure (Scheme map procedure)

查看:114
本文介绍了申请不是程序(方案图程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在R5RS中编写我自己的简化映射程序。简而言之,它需要一个过程和两个列表,并返回一个列表,其中列出了两个参数列表中每对对象所调用的过程的结果,直到空白为止。



这对算术运算符很有效,例如:

 (map2-rec +'(1 2 3)' 1 2 3 4))

但是,当我尝试传递一个匿名lambda函数(返回值我的两个?程序),它返回#t或#f,这不起作用。

 (define(map2-rec proc item1 item2)
(if(或(null?items1)(null?items2))
'()
(cons(proc(car items1)(car items2))
(map2-rec proc(cdr items1)(cdr items2)))))


(define(both?proc)
(lambda(item1 item2)
((如果(和项目1)(proc item2))
#t
#f))))

我在DrRacket中收到的具体错误是:

 应用程序:不是一个程序; 
预计一个可以
应用于参数
的过程:#t
arguments ...:[none]

如果有人能告诉我如何纠正这个错误,我会很高兴。我不明白为什么这个代码自己失败。

解决方案

两个?,围绕如果表达式。这应该解决它:

 (define(both?proc)
(lambda(item1 item2)
(if(and(proc item1)(proc item2))
#t
#f)))

现在您的程序按预期工作:

 (map2-rec +'(1 2 3) (1 2 3 4))
=> '(2 4 6)
(map2-rec(both?even?)'(1 2 3)'(1 2 3 4))
=> '(#f #t #f)


I am attempting to write my own simplified map procedure in R5RS. In short, it takes a procedure and two lists, and returns a list with the results of the procedure called on every pair of objects in the two argument lists, until either is empty.

This works fine for arithmetic operators, such as:

(map2-rec + '(1 2 3) '(1 2 3 4))

However, when I attempt to pass an anonymous lambda function (the return value of my both? procedure) which returns either #t or #f, this does not work.

(define (map2-rec proc items1 items2)
  (if (or (null? items1) (null? items2))
      '()
      (cons (proc (car items1) (car items2))
            (map2-rec proc (cdr items1) (cdr items2)))))


(define (both? proc)
  (lambda (item1 item2)
    ((if (and (proc item1) (proc item2))
         #t
         #f))))

The specific error I am receiving in DrRacket is:

application: not a procedure;  
expected a procedure that can be
applied to arguments   
given: #t   
arguments...: [none]

If someone could tell me how I can correct this error, I'd be very happy. I cannot understand why this code fails myself.

解决方案

There's an extra (and erroneous) pair of parentheses in both?, surrounding the if expression. This should fix it:

(define (both? proc)
  (lambda (item1 item2)
    (if (and (proc item1) (proc item2))
        #t
        #f)))

Now your procedure works as expected:

(map2-rec + '(1 2 3) '(1 2 3 4))
=> '(2 4 6)
(map2-rec (both? even?) '(1 2 3) '(1 2 3 4))
=> '(#f #t #f)

这篇关于申请不是程序(方案图程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆