Scheme 将值视为列表

Scheme 将值视为列表,scheme,Scheme,我想根据binary-e序列列出#t/#f语句。如果二进制e中的值为0,则输入lst的值应为#t,如果为1,则应为#f。 n参数是lst应该有多长。但是,它总是返回一个空列表。这是我的密码: (define (mysequence n) (define binary-e (list 0 0 1 1 0 0 1 0 0 0 1 0 1 1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 0 0 1 1 1 0 0 0 0 0

我想根据binary-e序列列出#t/#f语句。如果二进制e中的值为0,则输入lst的值应为#t,如果为1,则应为#f。 n参数是lst应该有多长。但是,它总是返回一个空列表。这是我的密码:

(define (mysequence n)                  
      (define binary-e (list 0 0 1 1 0 0 1 0 0 0 1 0 1 1 1 0 0 0 1 1 0 1 1 1 0 0 1 1 0 0 0 1 0 0 1 1 1 0 0 0 0 0 1 1 0 0 1 0 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 1 0 0 1 1 1 0 0 0 0 0 1 1 0 0 1 0 0 0 1 1 1 0 0 0 0 0 1 1 0 1 0 0 0 0 1 1))

         (define (makelist lst k)
            (cond((= k (- n 1)) lst)        
                ((= 0 (list-ref binary-e k)) (begin (cons #t lst) (makelist lst (+ k 1)) ))           
                ((= 1 (list-ref binary-e k)) (begin (cons #f lst) (makelist lst (+ k 1))))

            )   
         )


      (makelist '() 0)      
)       

谢谢你的帮助

您可以使用
map
轻松解决此问题:

(map (lambda (e)
       (if (= e 0) #t #f))
     binary-e)
甚至更短:

(map zero? binary-e)
(define (mysequence lst)
  (if (<???> lst)            ; if the list is empty
      <???>                  ; then return the empty list
      (cons                  ; else `cons`
       <???>                 ; if the first element in list is zero #t else #f
       (mysequence <???>)))) ; process the rest of the list
但是如果您需要从头开始编写解决方案,恐怕问题中的代码远不是正确答案。我会给你一些建议,并向你展示解决方案的正确结构,这样你可以自己找到答案(因为这看起来很像家庭作业),但你必须完全重新思考你的答案。首先,您不需要传递列表的大小:

(define (mysequence lst)
  (cond ((<???> lst)                  ; if the list is empty
         <???>)                       ; then return the empty list
        ((= <???> <???>)              ; if the first element in the list is 0
         (cons <???>                  ; then `cons` #t
               (mysequence <???>)))   ; and process the rest of the list
        ((= <???> <???>)              ; if the first element in the list is 1
         (cons <???>                  ; then `cons` #f
               (mysequence <???>))))) ; and process the rest of the list
(定义(mysequence lst)
(cond((lst);如果列表为空
);然后返回空列表
((=);如果列表中的第一个元素为0
(cons;然后是“cons”
(mysequence));并处理列表的其余部分
((=);如果列表中的第一个元素为1
(cons;然后是‘cons’
(我的顺序(())));并处理列表的其余部分
甚至更短:

(map zero? binary-e)
(define (mysequence lst)
  (if (<???> lst)            ; if the list is empty
      <???>                  ; then return the empty list
      (cons                  ; else `cons`
       <???>                 ; if the first element in list is zero #t else #f
       (mysequence <???>)))) ; process the rest of the list
(定义(mysequence lst)
(如果(lst);如果列表为空
;然后返回空列表
(反对;否则`反对`
;如果列表中的第一个元素为零#t else#f
(我的顺序);处理列表的其余部分
无论哪种方式,都可以这样称呼:

(define binary-e <???>) ; define the list outside the procedure
(mysequence binary-e)   ; and pass it along as a parameter
(定义二进制-e);在过程之外定义列表
(mysequence binary-e);并将其作为参数传递
您当前问题中的代码看起来像是为过程语言编写的-尤其是对此类问题使用
list ref
。你必须停止用C/C++/C#/Java或任何你常用的编程语言来思考问题,开始用Scheme的方式来思考问题——这有利于更具功能性的编程风格