2012-11-18 3 views
1

시퀀스 바이너리 -e에 따라 # t/# f 명령문의 목록을 만들고 싶다. binary-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 (lambda (e) 
     (if (= e 0) #t #f)) 
    binary-e) 

심지어 짧은 :

답변

3

당신은 쉽게 map를 사용하여 하나 해결할 수

(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 binary-e <???>) ; define the list outside the procedure 
(mysequence binary-e) ; and pass it along as a parameter 

(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 

심지어 짧은 : 우선, 당신은 목록의 크기를 전달할 필요가 없습니다 현재 문제의 코드는 절차 언어 용으로 작성된 것 같습니다. 특히 list-ref을 사용하면 이러한 종류의 문제에 적합하지 않습니다. C/C++/C#/Java 또는 일반적인 프로그래밍 언어가 무엇이든간에 생각을 멈추고보다 체계적인 프로그래밍 스타일을 선호하는 Scheme 방식으로 생각을 시작해야합니다.

관련 문제