2014-09-19 1 views
-2

숫자 목록 끝에 새 번호를 추가하는 함수를 작성하려고하지만 구문 오류를 추적하고 수정할 수 없습니다. 누군가 나를 도울 수 있습니까? 감사!어떻게 프로그램이 실행되지 않고 구문 오류가 발생합니까? (DrRacket/Scheme)

(define (add the-list element) 
(cond 
    ((empty? the-list) (list element)   
    (else (cons (first the-list) cons (add (rest the-list) element)))))) 

(check-expect (four (list 2 5 4) 1) (list 2 5 4 1)) ; four adds num at the end of lon 
+0

전화'체크 기대 ''add'를 사용하고''four ''를 사용해서는 안됩니다. –

답변

2

가 잘못 괄호의 몇 가지, 그리고 마지막에 그 두 번째 conscons 매개 변수를 기대하고, 잘못된 것입니다. 이 시도 :

(define (add the-list element) 
    (cond ((empty? the-list) (list element)) 
     (else (cons (first the-list) 
        (add (rest the-list) element))))) 

사용 라켓의 훌륭한 편집기가 제대로 코드를 포맷하고 들여 쓰기, 문제의이 종류는 쉽게 감지 할 수있다. 힌트 : 코드를 다시 들여 오기 위해 Ctrl + 을 사용하면 구문 오류를 발견하는 데 매우 유용합니다. 사이드 참고로, 동일한 절차는 다음과 같이, 기존의 절차를 사용하여 더 관용구 구현할 수 있습니다

(define (add the-list element) 
    (append the-list (list element))) 

또는이 같은 고차 절차를 사용하여 :

(define (add the-list element) 
    (foldr cons (list element) the-list))