Loops 使用循环返回列表

Loops 使用循环返回列表,loops,for-loop,scheme,lisp,racket,Loops,For Loop,Scheme,Lisp,Racket,我是个新手。我正在尝试使用循环返回列表列表。代码可以工作,但它只打印其中一个元素3次,而不计算其他条件,从而导致重复。如何循环遍历每个条件并为每个条件打印一个元素而不重复 (define position_ (lambda (list

我是个新手。我正在尝试使用循环返回列表列表。代码可以工作,但它只打印其中一个元素3次,而不计算其他条件,从而导致重复。如何循环遍历每个条件并为每个条件打印一个元素而不重复

(define position_                                                                                                                             
  (lambda
      (list_) 
    (let ([size 3]) ;
      (for/list ([binary (in-range 0 size)])
        (cond [(not (empty? list_))
               (cond((list-ref list_ 0) (cond ((equal? (list-ref list_ 0) 1) (list (vector-ref Table 0) HIGH)) ((equal? (list-ref list_ 0) 0) (list (vector-ref Table 0) LOW))))                 
                    ((list-ref list_ 1) (cond ((equal? (list-ref list_ 1) 1) (list (vector-ref Table 1) HIGH)) ((equal? (list-ref list_ 1) 0) (list (vector-ref Table 1) LOW)))) 
                    ((list-ref list_ 2) (cond ((equal? (list-ref list_ 2) 1) (list (vector-ref Table 2) HIGH)) ((equal? (list-ref list_ 2) 0) (list (vector-ref Table 2) LOW))))  
               )])
        ))))

您的
cond
只有一个术语。这意味着你已经做了这样的事情:

(cond [(not (empty? list_)) ...]
      [else 'pigs-are-flying]) ; what happens when the list is not empty
(cond [p1 c1]
      [else (cond [p2 c2]
                  [else a2])])
现在结果是猪在飞,这只是我的建议。事实上,这份报告没有具体说明这一点,这意味着一切都在进行中

还要知道,
cond
是方案的if-elseif-else,因此很少需要嵌套
cond
。。代码如下:

(cond [(not (empty? list_)) ...]
      [else 'pigs-are-flying]) ; what happens when the list is not empty
(cond [p1 c1]
      [else (cond [p2 c2]
                  [else a2])])
这是一种很难写的方式:

(cond [p1 c1]
      [p2 c2]
      [else a2])
如果你想这样做:

(cond [p1   
       (cond [p2 c2] 
             [else a2])]
      [else a1])
可以对谓词求反以获得相同的平坦行为:

(cond [(not p1) a1]
      [p2 c2]
      [else a2])

作为一种风格评论。结束括号不应该在它自己的行上。它应该是与它的朋友在前一行。这是人类读到的识别,没有括号。DrRacket会帮你做到这一点,所以没有必要养成坏习惯

您的
cond
只有一个术语。这意味着你已经做了这样的事情:

(cond [(not (empty? list_)) ...]
      [else 'pigs-are-flying]) ; what happens when the list is not empty
(cond [p1 c1]
      [else (cond [p2 c2]
                  [else a2])])
现在结果是猪在飞,这只是我的建议。事实上,这份报告没有具体说明这一点,这意味着一切都在进行中

还要知道,
cond
是方案的if-elseif-else,因此很少需要嵌套
cond
。。代码如下:

(cond [(not (empty? list_)) ...]
      [else 'pigs-are-flying]) ; what happens when the list is not empty
(cond [p1 c1]
      [else (cond [p2 c2]
                  [else a2])])
这是一种很难写的方式:

(cond [p1 c1]
      [p2 c2]
      [else a2])
如果你想这样做:

(cond [p1   
       (cond [p2 c2] 
             [else a2])]
      [else a1])
可以对谓词求反以获得相同的平坦行为:

(cond [(not p1) a1]
      [p2 c2]
      [else a2])

作为一种风格评论。结束括号不应该在它自己的行上。它应该是与它的朋友在前一行。这是人类读到的识别,没有括号。DrRacket会帮你做到这一点,所以没有必要养成坏习惯

为了让代码正常工作,我不得不修改逻辑和语法,不过现在它工作得很好

(define position_                                                                                                                             
  (lambda
      (list_) 
    (let ([size  (length list_) ]) ;
      (for/list ([binary (in-range 0 size)])
        (cond [(not (empty? list_))
               (cond[ (equal? binary 0) (if (equal? (list-ref list_ binary) 1) (list (vector-ref Table 0) HIGH) (list (vector-ref Table 0) LOW) )]
                     [ (equal? binary 1) (if (equal? (list-ref list_ binary) 1) (list (vector-ref Table 1) HIGH) (list (vector-ref Table 1) LOW))]
                     [ (equal? binary 2) (if (equal? (list-ref list_ binary) 1) (list (vector-ref Table 2) HIGH) (list (vector-ref Table 2) LOW))]

                    )
               ]
               ))
        )))

为了让代码正常工作,我不得不修改逻辑和语法,不过现在它工作得很好

(define position_                                                                                                                             
  (lambda
      (list_) 
    (let ([size  (length list_) ]) ;
      (for/list ([binary (in-range 0 size)])
        (cond [(not (empty? list_))
               (cond[ (equal? binary 0) (if (equal? (list-ref list_ binary) 1) (list (vector-ref Table 0) HIGH) (list (vector-ref Table 0) LOW) )]
                     [ (equal? binary 1) (if (equal? (list-ref list_ binary) 1) (list (vector-ref Table 1) HIGH) (list (vector-ref Table 1) LOW))]
                     [ (equal? binary 2) (if (equal? (list-ref list_ binary) 1) (list (vector-ref Table 2) HIGH) (list (vector-ref Table 2) LOW))]

                    )
               ]
               ))
        )))

是的,我怀疑嵌套可能是这种情况,我会调整代码,看看它是否有效。我还知道
p2
实际上意味着
(和(不是p1)p2)
,因为它只计算
p1
是否为false。因此,在
cond
中,只有一个术语结果表达式将被计算。是的,我怀疑嵌套可能是这种情况,我将调整代码,看看它是否有效。我还知道
p2
实际上意味着
(而不是p1)p2
,因为它只计算
p1
是否为false。因此,在
cond
中,只会计算结果表达式中的一个术语。