diff --git a/src/ikarus.boot b/src/ikarus.boot index eff73b6..cfd9ba3 100644 Binary files a/src/ikarus.boot and b/src/ikarus.boot differ diff --git a/src/ikarus.core.ss b/src/ikarus.core.ss index 39fe137..bede892 100644 --- a/src/ikarus.core.ss +++ b/src/ikarus.core.ss @@ -70,43 +70,6 @@ -(primitive-set! 'length - (letrec ([race - (lambda (h t ls n) - (if (pair? h) - (let ([h ($cdr h)]) - (if (pair? h) - (if (not (eq? h t)) - (race ($cdr h) ($cdr t) ls ($fx+ n 2)) - (error 'length "circular list ~s" ls)) - (if (null? h) - ($fx+ n 1) - (error 'length "~s is not a proper list" ls)))) - (if (null? h) - n - (error 'length "~s is not a proper list" ls))))]) - (lambda (ls) - (race ls ls ls 0)))) - - -(primitive-set! 'list-ref - (lambda (list index) - (define f - (lambda (ls i) - (cond - [($fxzero? i) - (if (pair? ls) - ($car ls) - (error 'list-ref "index ~s is out of range for ~s" index list))] - [(pair? ls) - (f ($cdr ls) ($fxsub1 i))] - [(null? ls) - (error 'list-rec "index ~s is out of range for ~s" index list)] - [else (error 'list-ref "~s is not a list" list)]))) - (unless (and (fixnum? index) ($fx>= index 0)) - (error 'list-ref "~s is not a valid index" index)) - (f list index))) - diff --git a/src/ikarus.lists.ss b/src/ikarus.lists.ss index 982103b..99a9e11 100644 --- a/src/ikarus.lists.ss +++ b/src/ikarus.lists.ss @@ -1,10 +1,12 @@ (library (ikarus lists) - (export $memq list? reverse last-pair memq memv member) + (export $memq list? length list-ref reverse last-pair + memq memv member) (import - (only (scheme) $car $cdr) + (only (scheme) $car $cdr $fx+ $fxsub1 $fxzero? $fx>=) - (except (ikarus) list? reverse last-pair memq memv member)) + (except (ikarus) list? reverse last-pair memq memv member + length list-ref)) (define $memq (lambda (x ls) @@ -26,7 +28,42 @@ (null? h)))]) (lambda (x) (race x x)))) - + (define length + (letrec ([race + (lambda (h t ls n) + (if (pair? h) + (let ([h ($cdr h)]) + (if (pair? h) + (if (not (eq? h t)) + (race ($cdr h) ($cdr t) ls ($fx+ n 2)) + (error 'length "circular list ~s" ls)) + (if (null? h) + ($fx+ n 1) + (error 'length "~s is not a proper list" ls)))) + (if (null? h) + n + (error 'length "~s is not a proper list" ls))))]) + (lambda (ls) + (race ls ls ls 0)))) + + (define list-ref + (lambda (list index) + (define f + (lambda (ls i) + (cond + [($fxzero? i) + (if (pair? ls) + ($car ls) + (error 'list-ref "index ~s is out of range for ~s" index list))] + [(pair? ls) + (f ($cdr ls) ($fxsub1 i))] + [(null? ls) + (error 'list-rec "index ~s is out of range for ~s" index list)] + [else (error 'list-ref "~s is not a list" list)]))) + (unless (and (fixnum? index) ($fx>= index 0)) + (error 'list-ref "~s is not a valid index" index)) + (f list index))) + (define reverse (letrec ([race (lambda (h t ls ac) @@ -128,5 +165,7 @@ (lambda (x ls) (race ls ls ls x)))) + + )