r3/experiment.tng
author Tony Garnock-Jones <tonygarnockjones@gmail.com>
Wed, 16 Jan 2019 17:15:58 +0000
changeset 438 1fe179d53161
parent 57 52eb7e976a66
permissions -rw-r--r--
Add missing primitive implementation for the plain interpreter.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
     1
"How does one *undefine* a bunch of behaviour?
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
     2
One possibility: delegate to the empty object, causing a DNU"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
     3
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
     4
define [
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
     5
  #map -> [(map f) -> [#nil -> #nil
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
     6
                       [#hd -> x #tl -> xs] -> [#hd -> (f x) #tl -> (map f xs)]]]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
     7
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
     8
  map: [(map f) -> [nil: #nil
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
     9
                    [hd: x tl: xs] -> [hd: (f x) tl: (map f xs)]]]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    10
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    11
  (#map f) -> [#nil -> #nil
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    12
               [hd: x tl: xs] -> [hd: (f x) tl: (#map f xs)]]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    13
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    14
  (map f #nil) -> #nil
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    15
  (map f [hd: x tl: xs]) -> [hd: (f x) tl: (#map f xs)]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    16
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    17
  "The thing at the head of these messages is bound to 0 - so (msg 0) is 'self'"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    18
  (#nil map: f) -> #nil
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    19
  ([hd: x tl: xs] map: f) -> [hd: f x tl: (xs map: f)]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    20
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    21
  (#nil #map -> f) -> #nil
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    22
  ([#hd -> x #tl -> xs] #map -> f) -> [#hd -> f x #tl -> (xs #map -> f)]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    23
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    24
  (#nil #map->f) -> #nil
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    25
  ([#hd->x #tl->xs] #map->f) -> [#hd -> f x #tl -> (xs #map->f)]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    26
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    27
  (#nil #map f) -> #nil
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    28
  ([#hd: x #tl: xs] #map f) -> [#hd: f x #tl: (xs #map f)]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    29
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    30
  "Dot is quote"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    31
  (.nil.map f): .nil
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    32
  ([.hd: x .tl: xs].map f): [.hd: f x .tl: xs.map f]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    33
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    34
  "Problem: x .map f - the .map is a generic-send, the f is a specific-send"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    35
  "Specific-send is going to be rare, perhaps?"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    36
  "Syntax for it? x .map @ f"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    37
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    38
  "Alternative is to require messages to be bound, to either a generic-function
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    39
   or a regular lambda"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    40
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    41
  "Another alternative is to rearrange the order of arguments"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    42
  "(The implicit receiver is the /subject/)"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    43
  (.map f .nil): .nil
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    44
  (.map f [.hd: x .tl: xs]): [.hd: f x .tl: map f xs]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    45
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    46
  "Desugared, the above reads:"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    47
  .map: [f: [.nil: .nil
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    48
             [.hd: x .tl: xs]: [.hd f x .tl: map f xs]]]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    49
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    50
  "Yet another option is to make it a head-n-ary message (with the '0' argument)"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    51
  (.nil .map: f): .nil
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    52
  ([.hd: x .tl: xs] .map: f): [.hd: f x .tl: (xs.map: f)]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    53
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    54
  "Desugared:"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    55
  (0: .nil             .map: f): .nil
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    56
  (0: [.hd: x .tl: xs] .map: f): [.hd f x .tl: (xs.map: f)]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    57
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    58
  "So far, so good. What about, for the sugared/curried form, lifting the quote-requirement
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    59
   on the function operator position?"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    60
  (map f .nil): .nil
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    61
  (map f [.hd: x .tl: xs]): [.hd: f x .tl: map f xs]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    62
  "No good: if we want to be able to curry regular functions, we'll need some way
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    63
   of matching literals in LHS pos. This drop-the-quote-requirement only works
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    64
   for the global context object."
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    65
];;
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    66
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    67
(arg.hd)
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    68
(arg.tl)
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    69
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    70
"a pattern:" [(a b): _]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    71
"-->" (arg (a b))
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    72
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    73
define fold-left [
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    74
  kons: [knil: [.nil            : knil
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    75
                [.hd: h .tl: t] : (fold-left kons (kons h knil) t)]]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    76
];;
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    77
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    78
define fold-left [
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    79
  kons: [lp knil: [.nil: knil
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    80
                   [.hd: h .tl: t]: lp (kons h knil) t]]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    81
];;
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    82
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    83
"Using the Curry sugar"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    84
define (fold-left kons) [lp
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    85
  (knil .nil): knil
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    86
  (knil [.hd: h .tl: t]): lp (kons h knil) t
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    87
];;
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    88
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    89
"Variant"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    90
define fold-left [
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    91
  kons: [lp (knil .nil): knil
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    92
            (knil [.hd: h .tl: t]): lp (kons h knil) t]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    93
];;
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    94
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    95
map [x: x + 1] ([1, 2, 3] asList);;
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    96
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    97
"Oo! You can use the named-self syntax to get hold of the context!"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    98
"Er, degenerate cases notwithstanding"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
    99
let [here] in ...
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   100
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   101
"DAMN letrec is a problem piece of syntax"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   102
letrec: [(.map f .nil): .nil
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   103
         (.map f [.hd: h .tl: t]): [.hd: f h .tl: map f t]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   104
         .toList: [[]: .nil
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   105
                   [h ; t]: [.hd: h .tl: toList t]]] "yikes"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   106
in: map [x: x] (toList [1 2 3])
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   107
;;
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   108
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   109
map [x: x + 1] (toList [1 2 3]);;
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   110
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   111
"To lift something:" {lifted} <- val
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   112
"To drop something:" val <- {lifted}
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   113
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   114
"iota"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   115
define (..) [
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   116
  start: [.inf+: [loop x: [.first: x .rest: loop (x + 1)]] start
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   117
          .inf-: [loop x: [.first: x .rest: loop (x - 1)]] start
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   118
          end: (let op = (.if: start > end .then: (+) .else: (-)),
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   119
                [loop x: (.if: x = end
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   120
                          .then: .end
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   121
                          .else: [.first: x .rest: loop (op x 1)])] start)]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   122
];;
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   123
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   124
"What if, for messages sent implicitly to the context, we allow quoteless
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   125
literals, to get simpler-looking selectors?"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   126
define (..) [
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   127
  start: [.inf+: [loop x: [.first: x .rest: loop (x + 1)]] start
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   128
          .inf-: [loop x: [.first: x .rest: loop (x - 1)]] start
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   129
          end: (let op = (if: start > end then: (+) else: (-)),
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   130
                [loop x: (if: x = end
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   131
                          then: .end
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   132
                          else: [.first: x .rest: loop (op x 1)])] start)]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   133
];;
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   134
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   135
"Now, Smalltalk-style conditionals, using head-n-ary syntax:"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   136
define (..) [
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   137
  start: [.inf+: [loop x: [.first: x .rest: loop (x + 1)]] start
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   138
          .inf-: [loop x: [.first: x .rest: loop (x - 1)]] start
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   139
          end: (let op = (start > end ifTrue: (+) ifFalse: (-)),
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   140
                [loop x: (x = end
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   141
                            ifTrue: .end
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   142
                            ifFalse: [.first: x .rest: loop (op x 1)])] start)]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   143
];;
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   144
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   145
"Curried: doesn't quite work because of the need to quote the operator"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   146
define [
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   147
  (start .. .inf+): [loop x: [.first: x .rest: loop (x + 1)]] start
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   148
  (start .. .inf-): [loop x: [.first: x .rest: loop (x - 1)]] start
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   149
  (start .. end): (let op = (start > end ifTrue: (+) ifFalse: (-)),
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   150
                   [loop x: (x = end
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   151
                               ifTrue: .end
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   152
                               ifFalse: [.first: x .rest: loop (op x 1)])] start)
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   153
];;
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   154
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   155
define ($) [f: f];;
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   156
"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   157
f $ a
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   158
--> ($) f a
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   159
--> f a
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   160
"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   161
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   162
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   163
"
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   164
let pat = val,
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   165
body
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   166
-->
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   167
[pat: body] val
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   168
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   169
... which makes the scoping clear. (It's like let in scheme.)
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   170
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   171
letrec pat = val,
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   172
body
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   173
-->
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   174
well nothing really. It has to be primitive?
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   175
What about trying a named-let variant?
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   176
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   177
let loop (x = start): [.first: x .rest: loop (x + 1)]
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   178
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   179
Yuck.
52eb7e976a66 More experimental notes.
Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
parents:
diff changeset
   180
"