方案递归循环错误值和变量绑定 [英] Scheme Recursion Loop Incorrect Values and Variable Binding

查看:122
本文介绍了方案递归循环错误值和变量绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里提出了一个关于我正在工作的hang子游戏的问题。



递归方案功能值错误



我觉得hang子手部分令人困惑我的真正问题和疑问。我的问题是,我从递归循环中调用各种定义的函数,并得到不正确的值。当我自己调用这些相同的功能(不是递归循环)时,它们按预期工作。我知道这是我忽略的一个东西,或者一个可变的绑定问题正在发生,我需要一个解决方法。



首先在这里重现问题代码:


$ b(显示零条件 。$)

(其他
(显示\\\
\\\

(显示列表a_list,)
(显示
(显示(car a_constant))
(显示\\\

(显示如果上面的字母在列表中,结果应为true\\\

(显示(包含?a_list(car a_constant)))
(显示\\\

(显示输入一个字母:)
(recurse cons(symbol-> string(read))a_list)( - a_number 1))



这是递归循环中使用的定义:

 (define(display-list a_list separater)

(if(null? a_list)

(显示)

(开始

(显示(car a_list))

(null?(cdr a_list))

(显示)

(显示分隔符))

(display-list(cdr a_list)分离器)







(define(contains?list item)
; (显示\\\

;(显示列表)
;(显示\\\

;(显示项)
;(显示\\\

(cond((empty?list)
#f

((eq?(first list)item)
#t

(其他
(包含?(休息列表)项)





(定义a_constant' (notworking))
(define test_li_1'(nb))
(定义test_li_2'(ab))

跑了:

 > (包含?a_constant(car test_li_1))

#t
> (包含?a_constant(car test_li_2))

#f

正如预期的那样。



当我运行递归循环这是我得到的:

 > (recurse test_li_2 2)


a,b
n
如果上面的字母在列表中,结果应为true
#f

输入一封信:n


n,a,b
n
如果上面的字母在列表中,结果应为true
#f

在我看来,第一个输出是正确的,但第二个输出是不正确的。 p>

当我使用test_li_1运行时,它总是评估为true,因为'n'总是在我的a_list中。我通过几个测试集合在一起,是递归函数不断使用我最初传入列表,不使用附加的版本进行函数调用,这不是我想要的。我也认为这不是应该如何工作吗?不应该使用我的传递价值,而不是几个递归层次的价值?

解决方案

报告的问题是因为您所发生的问题're using eq? 来测试字符串相等性。从文档中:


(eq?v1 v2)→布尔值:返回 #t 如果 v1 v2 指的是同一个对象, #f 否则


所以你看, eq?正在测试身份,而不是为了等级。为此,您应该使用 string =? equal? 。修改包含?程序如下,它将工作:

 (define(contains?list item)
(cond
((empty?list)#f)
((string =?(first list)item)#t)
(其他(包含(休息列表)项))))


I started a question here about a hangman game I am working on.

Recursive Scheme Function Value Error

I feel that the hangman portion is confusing people from my real issue and question. My problem is, I call various defined functions from within my recursion loop and get incorrect values. When I call these same function by themselves (not in a recursive loop) they work as expected. I know it is either something I am overlooking or a variable binding issue is going on that I need a workaround for.

First here a reproduction of the problem code:

(define (recurse a_list a_number)
       (cond ((= a_number 0)
                 (display "Zero Condition.")
              )
             (else
                 (display "\n\n")
                 (display-list a_list ",")
                 (display "\n")
                 (display (car a_constant))
                 (display "\n")
                 (display "If the above letter is in the list, result should be true\n")
                 (display (contains? a_list (car a_constant)))
                 (display "\n")
                  (display "Enter a letter:")
                  (recurse (cons (symbol->string (read)) a_list) (- a_number 1))
              )
        )
   )

Here are my definitions used inside the recursive loop:

(define (display-list a_list separater)

   (if (null? a_list)

      (display "")

      (begin 

         (display (car a_list))

         (if (null? (cdr a_list))

            (display "")

            (display separater))

         (display-list (cdr a_list) separater)

      )

   )

)

(define (contains? list item)
  ;(display "\n")
  ;(display list)
  ;(display "\n")
  ;(display item)
  ;(display "\n")
  (cond ((empty? list)
       #f
     )
    ((eq? (first list) item)
       #t
     )
    (else
       (contains? (rest list) item)
     )
   )
 )


(define a_constant '("n" "o" "t" "w" "o" "r" "k" "i" "n" "g"))
(define test_li_1 '("n" "b"))
(define test_li_2 '("a" "b"))

This is what I ran:

> (contains? a_constant (car test_li_1))

#t
> (contains? a_constant (car test_li_2))

#f

It works as expected.

When I run the recursive Loop this is what I get:

> (recurse test_li_2 2)


a,b
n
If the above letter is in the list, result should be true
#f

Enter a letter:n


n,a,b
n
If the above letter is in the list, result should be true
#f

In my mind, the first output is correct, but the second one is not.

When I run with test_li_1 it always evaluates to true which, it should since 'n' is always in my a_list. What I have put together through several tests, is the recursive function keeps using my initially passed in list and does not use the appended version for function calls, which is not what I want it to do. I also think that is not how it should work right? Shouldn't my passed in value be used and not a value from several recursive levels up? I'm testing this all in Dr. Racket with #lang racket in case that matters.

解决方案

The problem reported occurs because you're using eq? to test for string equality. From the documentation:

(eq? v1 v2) → boolean? : Return #t if v1 and v2 refer to the same object, #f otherwise.

So you see, eq? is testing for identity, not for equality. For that, you should use string=? or equal?. Modify the contains? procedure as follows, and it'll work:

(define (contains? list item)
  (cond 
    ((empty? list) #f)
    ((string=? (first list) item) #t)
    (else (contains? (rest list) item))))

这篇关于方案递归循环错误值和变量绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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