aboutsummaryrefslogtreecommitdiffstats
path: root/examples/fibonacci.fp
blob: 211a4d3dc4b3b78b698b827c2cf77d225539c9df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
  (8 +)               $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

  16 0 range
  ^fibonacci map
  ^print each
)