2016-10-15 3 views
2

목록의 첫 번째 요소로 목록을 정렬하는 중입니다.
(sort (list '(2 1 6 7)'(4 1 2 7)) '(1 1))))스키마의 첫 번째 요소로 목록 목록 정렬

예상 출력 => ('(1) '(2 1 6 7)'(4 3 1 2 3 4 5))

I가 사용되는 알고리즘은 거품 인 종류. 그리고 목록을 다루도록 수정했습니다. 그러나 코드는 컴파일되지 않습니다. 오류는

mcar: contract violation 
    expected: mpair? 
    given: 4 

내 코드를 수정하고 설명 할 수 있습니까? 고맙습니다.

(define (bubble L) 
     (if (null? (cdr L)) 
      L  
      (if (< (car (car L)) (car (cadr L))) 
       (list (car L) 
         (bubble (car (cdr L)))) 
       (list (cadr L) 
         (bubble (cons (car (car L)) (car (cddr L)))))))) 

    (define (bubble-sort N L)  
     (cond ((= N 1) (bubble L)) 
       (else 
       (bubble-sort (- N 1) (bubble L))))) 

    (define (bubble-set-up L) 
     (bubble-sort (length L) L)) 


    (define t3 (list '(2 1 6 7) '(4 3 1 2 4 5) '(1 2 3) '(1 1))) 
    (bubble-set-up t3) 

답변

2

몇 가지 실수를 수정했습니다. 최소한 하나의 실수가 남아 있습니다. L에 요소가 하나만있는 경우를 고려하십시오.

#lang r5rs 

(define (bubble L) 
    (if (null? (cdr L)) 
     L  
     (if (< (car (car L)) (car (cadr L))) 
      (cons (car L) 
       (bubble (cdr L))) 
      (cons (cadr L) 
       (bubble (cons (car L) (cddr L))))))) 

(define (bubble-sort N L)  
    (cond ((= N 1) (bubble L)) 
     (else 
     (bubble-sort (- N 1) (bubble L))))) 

(define (bubble-set-up L) 
    (bubble-sort (length L) L)) 


(define t3 (list '(2 1 6 7) '(4 3 1 2 4 5) '(1 2 3) '(1 1))) 
(display (bubble-set-up t3)) 
(newline) 
2

(sort (lambda (x y)(< (car x)(car y))) <YOUR_LIST>) 어때요?