parent
c546646cd4
commit
95901f3a5f
@ -0,0 +1,56 @@
|
|||||||
|
(
|
||||||
|
; core utilities
|
||||||
|
($x ^x ^x) $dup
|
||||||
|
($_) $drop
|
||||||
|
($x $y ^x ^y) $swap
|
||||||
|
($a $b $c ^b ^a ^c) $rot
|
||||||
|
(sub) $-
|
||||||
|
(0 swap - -) $+
|
||||||
|
(()) $nil
|
||||||
|
(nil eq) $null?
|
||||||
|
($x x) $force
|
||||||
|
(10 emit) $cr
|
||||||
|
(print) $print
|
||||||
|
(4 +) $cell+
|
||||||
|
($a $d 2 alloc $p
|
||||||
|
^a ^p poke
|
||||||
|
^d ^p cell+ poke
|
||||||
|
^p
|
||||||
|
) $cons
|
||||||
|
(peek) $car
|
||||||
|
(cell+ peek) $cdr
|
||||||
|
|
||||||
|
; if-stmt
|
||||||
|
($c $t $f c ^f ^t rot cswap $_ force) $if
|
||||||
|
($f $t $c $fn ^f ^t ^c fn) $endif
|
||||||
|
|
||||||
|
; range
|
||||||
|
($self $start $end
|
||||||
|
^if (^start ^end eq)
|
||||||
|
^nil
|
||||||
|
(^start ^end ^start 1 + self swap cons)
|
||||||
|
endif
|
||||||
|
) $range
|
||||||
|
|
||||||
|
; map [$fn $list -> $out-list]
|
||||||
|
($self $fn $list
|
||||||
|
^if (^list null?)
|
||||||
|
^nil
|
||||||
|
(^list car fn ^list cdr ^fn self swap cons)
|
||||||
|
endif
|
||||||
|
) $map
|
||||||
|
|
||||||
|
; each [$fn $list]
|
||||||
|
($fn (fn ^nil) map drop) $each
|
||||||
|
|
||||||
|
; implementation
|
||||||
|
(0 1 ($self $a $b $n
|
||||||
|
^if (^n 0 eq) (^b) (
|
||||||
|
^n 1 - ^a ^b + ^b self
|
||||||
|
) endif
|
||||||
|
) force) $fibonacci
|
||||||
|
|
||||||
|
30 10 range
|
||||||
|
^fibonacci map
|
||||||
|
^print each
|
||||||
|
)
|
@ -1,56 +1,25 @@
|
|||||||
(
|
(
|
||||||
; core utilities
|
; core utilities
|
||||||
($x ^x ^x) $dup
|
|
||||||
($_) $drop
|
($_) $drop
|
||||||
($x $y ^x ^y) $swap
|
($x $y ^x ^y) $swap
|
||||||
($a $b $c ^b ^a ^c) $rot
|
($a $b $c ^b ^a ^c) $rot
|
||||||
(sub) $-
|
(sub) $-
|
||||||
(0 swap - -) $+
|
(0 swap - -) $+
|
||||||
(()) $nil
|
|
||||||
(nil eq) $null?
|
|
||||||
($x x) $force
|
($x x) $force
|
||||||
|
(32 emit) $bl
|
||||||
(10 emit) $cr
|
(10 emit) $cr
|
||||||
(print) $print
|
|
||||||
(4 +) $cell+
|
|
||||||
($a $d 2 alloc $p
|
|
||||||
^a ^p poke
|
|
||||||
^d ^p cell+ poke
|
|
||||||
^p
|
|
||||||
) $cons
|
|
||||||
(peek) $car
|
|
||||||
(cell+ peek) $cdr
|
|
||||||
|
|
||||||
; if-stmt
|
; if-stmt
|
||||||
($c $t $f c ^f ^t rot cswap $_ force) $if
|
($c $t $f c ^f ^t rot cswap $_ force) $if
|
||||||
($f $t $c $fn ^f ^t ^c fn) $endif
|
($f $t $c $fn ^f ^t ^c fn) $endif
|
||||||
|
|
||||||
; range
|
; range
|
||||||
($self $start $end
|
($self $fn $start $end
|
||||||
^if (^start ^end eq)
|
^if (^start ^end eq) ()
|
||||||
^nil
|
(^start fn ^end ^start 1 + ^fn self)
|
||||||
(^start ^end ^start 1 + self swap cons)
|
|
||||||
endif
|
endif
|
||||||
) $range
|
) $each
|
||||||
|
|
||||||
; map [$fn $list -> $out-list]
|
10 0 ($i 10 0 ($j ^i 48 + emit ^j 48 + emit bl) each) each
|
||||||
($self $fn $list
|
cr
|
||||||
^if (^list null?)
|
|
||||||
^nil
|
|
||||||
(^list car fn ^list cdr ^fn self swap cons)
|
|
||||||
endif
|
|
||||||
) $map
|
|
||||||
|
|
||||||
; each [$fn $list]
|
|
||||||
($fn (fn ^nil) map drop) $each
|
|
||||||
|
|
||||||
; implementation
|
|
||||||
(0 1 ($self $a $b $n
|
|
||||||
^if (^n 0 eq) (^b) (
|
|
||||||
^n 1 - ^a ^b + ^b self
|
|
||||||
) endif
|
|
||||||
) force) $fibonacci
|
|
||||||
|
|
||||||
10 1 range
|
|
||||||
^fibonacci map
|
|
||||||
^print each
|
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue