list - Some misunderstanding between me and Scheme -
i'm started learning scheme , can't quite understand why function not work:
;(define (sort l) (define (sorted? l) (if (= (length l) 2) ; if simple list: (if (< (head l) (tail l)) #t #f) ; if complex list: (if (and (< (head l) (head (tail l))) (sorted? (tail l))) #t #f)))
output:
(sorted? (1 0)) . . procedure application: expected procedure, given: 1; arguments were: 0 (sorted? '(1 0)) . . <: expects type 2nd argument, given: (0); other arguments were: 1
racket, r5rs
this because compare integer , list, cadr
or if (< (head l) (head (tail l)))
instead of if (< (head l) (tail l))
after line ; if simple list
have more chances work you.
the following definition works me:
(define (sorted? xs) (cond ((<= (length xs) 1) #t) ((< (car xs) (cadr xs)) (sorted? (cdr xs))) (else #f)))
Comments
Post a Comment