File: prism-sml.html

package info (click to toggle)
node-prismjs 1.30.0%2Bdfsg%2B~1.26.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,220 kB
  • sloc: javascript: 27,628; makefile: 9; sh: 7; awk: 4
file content (43 lines) | stat: -rw-r--r-- 1,158 bytes parent folder | download | duplicates (3)
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
<h2>Full example</h2>
<pre><code>(* source: https://github.com/HarrisonGrodin/ml-numbers/blob/ba35c763092052e391871edf224f17474c6231b1/src/Rational.sml *)

structure Rational :> RATIONAL =
  struct
    type t = int * int  (* (a,b) invariant: a,b coprime; b nonnegative *)

    local
      val rec gcd = fn
        (m,0) => m
      | (m,n) => gcd (n, m mod n)
    in
      infix 8 //
      val op // = fn (x,y) => (
        let
          val gcd = gcd (x,y)
        in
          (x div gcd, y div gcd)
        end
      )
    end

    val show = Fn.id

    val zero = (0,1)
    val one  = (1,1)

    val eq : t * t -> bool = (op =)
    val compare = fn ((a,b),(x,y)) => Int.compare (a * y, b * x)
    val toString = fn (x,y) => Int.toString x ^ " // " ^ Int.toString y
    val percent =
      Fn.curry (Fn.flip (op ^)) "%"
      o Int.toString
      o (fn (a,b) => (100 * a) div b)

    val op + = fn ((a,b),(x,y)) => (a * y + b * x) // (b * y)
    val ~ = fn (a,b) => (~a,b)
    val op - = fn (r1,r2) => r1 + ~r2

    val op * = fn ((a,b),(x,y)) => (a * x) // (b * y)
    val inv = Fn.flip (op //)
    val op / = fn (r1,r2) => r1 * inv r2
  end</code></pre>