(define (iter a count)
(if (null? a)
count
- (iter (cdr a) (+ count 1))))
+ (iter (cdr a) (fix:+ count 1))))
(iter l 0))
; Join two lists together
(define (sum n)
(define (sum-iter total count maxcount)
- (if (> count maxcount)
+ (if (fix:> count maxcount)
total
- (sum-iter (+ total count) (+ count 1) maxcount)))
+ (sum-iter (fix:+ total count) (fix:+ count 1) maxcount)))
(sum-iter 0 1 n))
; Recursive summation. Use this to compare with tail call
; optimized iterative algorithm.
(define (sum-recurse n)
- (if (= n 0)
+ (if (fix:= n 0)
0
- (+ n (sum-recurse (- n 1)))))
+ (fix:+ n (sum-recurse (fix:- n 1)))))
;; MISC
charlist>symbol
; make-primitive string->symbol
+:noname ( charlist -- string )
+ 2dup 1 ensure-arg-count
+
+ car nil? if
+ 2drop
+ nil nil cons
+ drop string-type
+ exit
+ then
+
+ pair-type ensure-arg-type
+
+ duplicate-charlist
+ drop string-type
+; make-primitive list->string
+
+:noname ( string -- charlist )
+ 2dup 1 ensure-arg-count
+ car string-type ensure-arg-type
+
+ drop pair-type
+
+ 2dup car nil? if
+ 2swap 2drop
+ else
+ 2drop
+ duplicate-charlist
+ then
+
+; make-primitive string->list
+
\ }}}
\ ==== Numeric types ==== {{{
\ ==== Input/Output ==== {{{
+:noname ( -- port )
+ console-i/o-port obj@
+; 0 make-fa-primitive console-i/o-port
+
+:noname ( -- port )
+ current-input-port obj@
+; 0 make-fa-primitive current-input-port
+
+: charlist>cstr ( charlist addr -- n )
+
+ dup 2swap ( origaddr addr charlist )
+
+ begin
+ nil? false =
+ while
+ 2dup cdr 2swap car
+ drop ( origaddr addr charlist char )
+ -rot 2swap ( origaddr charlist addr char )
+ over !
+ 1+ -rot ( origaddr nextaddr charlist )
+ repeat
+
+ 2drop ( origaddr finaladdr )
+ swap -
+;
+
:noname ( args -- finalResult )
drop pair-type
pad charlist>cstr
make-type symbol-type
make-type primitive-proc-type
make-type compound-proc-type
-make-type fileport-type
+make-type port-type
: istype? ( obj type -- obj bool )
over = ;
drop ;
: fid>fileport ( fid -- fileport )
- fileport-type ;
+ port-type ;
: open-input-file ( addr n -- fileport )
r/o open-file drop fid>fileport
;
objvar console-i/o-port
-0 fileport-type console-i/o-port obj!
+0 port-type console-i/o-port obj!
objvar current-input-port
console-i/o-port obj@ current-input-port obj!
primitive-proc-type istype? if printprim exit then
compound-proc-type istype? if printcomp exit then
none-type istype? if printnone exit then
+ port-type istype? if printport exit then
recoverable-exception throw" Tried to print object with unknown type."
; is print
\ ---- Loading files ---- {{{
-: charlist>cstr ( charlist addr -- n )
-
- dup 2swap ( origaddr addr charlist )
-
- begin
- nil? false =
- while
- 2dup cdr 2swap car
- drop ( origaddr addr charlist char )
- -rot 2swap ( origaddr charlist addr char )
- over !
- 1+ -rot ( origaddr nextaddr charlist )
- repeat
-
- 2drop ( origaddr finaladdr )
- swap -
-;
-
: load ( addr n -- finalResult )
open-input-file