old-language.mty (2502B)
1 ns main; 2 3 from namespace use stream_data; 4 5 let Point :: x:f, y:f; 6 7 let Player :: { 8 pos:Point; 9 health:f; 10 age:i; 11 fx distance : Player -> (f, f) :: (abs(Player.pos.x - pos.x), abs(Player.pos.y - pos.y)); 12 } 13 14 fx distance p1:Player, p2:Player -> f, f :: 15 (abs(p1.pos.x - p2.pos.x), abs(p1.pos.y - p2.pos.y)); 16 17 pub dupe :: (str, int) = (*) 18 19 let Player :: struct { 20 Point pos; 21 dec health; 22 int age; 23 bool alive; 24 pub is_old = age > 60; 25 } 26 27 let RSVP :: enum(str) { 28 29 } 30 31 32 enum(str) RSVP :: YES, NO; 33 34 pub struct Person :: 35 str name, 36 str domain, 37 int age, 38 dec size, 39 Bool alive, 40 RSVP invited, 41 fx is_alive :: -> bool = .alive, 42 fn email :: -> str = "$name@${domain}", 43 fx knows :: Person p -> bool = .in_contact_with(p); 44 45 46 fx new_external_person :: str name -> Person = Person(name: name, alive: true); 47 48 49 fx(Float, Float) distance(Point a, b) :: (abs(a.x - b.x), abs(a.y - b.y)) 50 51 fx distance :: Point a, b -> Float, Float = (abs(a.x - b.x), abs(a.y - b.y)); 52 53 54 let Person :: struct = str name, int age, dec size, bool alive, RSVP invite; 55 56 fx in_space :: char c -> bool = (c == ' ' || c == '\r' || c == '\n' || c == '\t'); 57 58 fx rev :: str x -> str = x[.. by -1]; 59 60 fx trim :: str x -> str = x 61 | x[drop while in_space] 62 | rev 63 | x[drop while in_space] 64 | rev; 65 66 67 fx say_hello() :: printf("hello\n") 68 69 70 fx distance(Point a, b) :: 71 (abs(a.x - b.x), abs(a.y - b.y)) 72 73 fx distance(Point a, b -> (dec, dec)) 74 75 fx is_even(ref int -> bool) = (% 2 == 0) 76 77 78 fx distance(Point a, b) -> (f, f) = (abs(a.x - b.x), abs(a.y - b.y)) 79 80 ---------- 81 82 fx distance(a:pos, b:pos) (f, f) = (abs(a.x - b.x), abs(a.y - b.y)) 83 84 let Player = { 85 pos:Point, 86 health:f, 87 age:i, 88 alive:b = true 89 fx distance :: fx(p2:Player) (f, f) = (abs(pos.x - p2.pos.x), abs(pos.y - p2.pos.y)) 90 } 91 92 let even : i -> b :: % 2 == 0 93 94 let add : i, i -> i :: + 95 96 let add_mult_by_5 : i, i -> i :: + * 5 97 98 let add_mult_by_6 : a:i, b:i -> i :: (a + b) * 6 99 100 fx add_mult_by_x (a:i, b:i, x:i) i = (a + b) * x 101 102 from libc use printf[ffi] 103 104 fx say_hello :: printf("hello\n") 105 106 fx say_hello = printf("hello\n") 107 108 109 fx rev(text:s) -> s = text[.. by -1]; 110 111 112 113 fx rev : 114 s -> s = s[.. by -1] 115 116 fn is_space : c -> b = 117 c == ' ' || c == '\r' || c == '\n' || c == '\t'; 118 119 fx trim(text:s) -> s = 120 len(text) == 0 ? text : 121 text 122 | t => t[drop while is_space] 123 | rev 124 | t => t[drop while is_space] 125 | rev; 126 127 is_space :: c -> b = c == ' ' || c == '\r' || c == '\n' || c == '\t'; 128 129 130 pub let is_space : c -> b = (c == ' ' || c == '\r' || c == '\n' || c == '\t'); 131 132 is_space :: char c -> bool = 133 134