Skip to content

core — examples

← all topics · 698 examples · page 10 of 14 · raw source ↓

Runs `test` → "foo"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Function_Mixed_Param_Types   topic: core   status: verified
// verified behavior: Test.test(...) == "foo"

namespace Test

func test(a: int, b: bool, c: string) -> string {
    if b {
        return c + ":" + a.ToString()
    }
    return c
}

Runs `test` → 7

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Function_Returns_RefData_Pin   topic: core   status: verified
// verified behavior: Test.test(...) == 7

namespace Test

class Card {
    pub face: int
    init(f: int) { self.face = f }
}

func make(n: int) -> Card = Card(n)

func test() -> int {
    let c = make(7)
    return c.face
}

Runs `test` → 5050

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Function_Tail_Recursive_Style   topic: core   status: verified
// verified behavior: Test.test(...) == 5050

namespace Test

func sum_to(n: int, acc: int) -> int {
    if n == 0 {
        return acc
    }
    return sum_to(n - 1, acc + n)
}

func test() -> int = sum_to(100, 0)

Runs `test` → 21

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Function_With_Six_Params   topic: core   status: verified
// verified behavior: Test.test(...) == 21

namespace Test

func test(a: int, b: int, c: int, d: int, e: int, f: int) -> int = a + b + c + d + e + f

Runs `test` → "negative"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::If_Else_Returning_String   topic: core   status: verified
// verified behavior: Test.test(...) == "negative"

namespace Test

func test(n: int) -> string {
    if n == 0 {
        return "zero"
    } else if n > 0 {
        return "positive"
    } else {
        return "negative"
    }
}

ILEmitterTests3__If_Nested_Inside_If

core runnable verified

Runs `test` → 3

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::If_Nested_Inside_If   topic: core   status: verified
// verified behavior: Test.test(...) == 3

namespace Test

func test(a: int, b: int) -> int {
    if a > 0 {
        if b > 0 {
            return 1
        }
        return 2
    }
    return 3
}

Runs `test` → 10

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::If_No_Else_Falls_Through_Cleanly   topic: core   status: verified
// verified behavior: Test.test(...) == 10

namespace Test

func test(flag: bool) -> int {
    var x = 10
    if flag {
        x = 100
    }
    return x
}

Runs `test` → false

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::If_With_Compound_Condition_And   topic: core   status: verified
// verified behavior: Test.test(...) == false

namespace Test

func test(a: int, b: int) -> bool {
    if a > 0 && b > 0 {
        return true
    }
    return false
}

Runs `test` → false

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::If_With_Compound_Condition_Or   topic: core   status: verified
// verified behavior: Test.test(...) == false

namespace Test

func test(a: int, b: int) -> bool {
    if a > 0 || b > 0 {
        return true
    }
    return false
}

ILEmitterTests3__If_With_Not_Operator

core runnable verified

Runs `test` → 1

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::If_With_Not_Operator   topic: core   status: verified
// verified behavior: Test.test(...) == 1

namespace Test

func test(b: bool) -> int {
    if !b {
        return 1
    }
    return 0
}

Runs `test` → -1

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::IfElse_Picks_Else_Branch   topic: core   status: verified
// verified behavior: Test.test(...) == -1

namespace Test

func test(x: int) -> int {
    if x > 0 {
        return 1
    } else {
        return -1
    }
}

Runs `test` → 3

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Int_Division_Truncates   topic: core   status: verified
// verified behavior: Test.test(...) == 3

namespace Test

func test() -> int = 7 / 2

Runs `test` → -2147483648

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Int_Max_Plus_One_Overflows   topic: core   status: verified
// verified behavior: Test.test(...) == -2147483648

namespace Test

func test() -> int {
    let max = 2147483647
    return max + 1
}

ILEmitterTests3__Linear_Search_Idiom

core runnable verified

Runs `contains` → true

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Linear_Search_Idiom   topic: core   status: verified
// verified behavior: Test.contains(...) == true

namespace Test

func contains(target: int, lo: int, hi: int) -> bool {
    var i = lo
    while i < hi {
        if i == target {
            return true
        }
        i = i + 1
    }
    return false
}

Runs `test` → 1000

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Long_Loop_Counts_To_1000   topic: core   status: verified
// verified behavior: Test.test(...) == 1000

namespace Test

func test() -> int {
    var i = 0
    var count = 0
    while i < 1000 {
        count = count + 1
        i = i + 1
    }
    return count
}

ILEmitterTests3__Max_Of_Three_Values

core runnable verified

Runs `max3` → 9

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Max_Of_Three_Values   topic: core   status: verified
// verified behavior: Test.max3(...) == 9

namespace Test

func max3(a: int, b: int, c: int) -> int {
    var m = a
    if b > m { m = b }
    if c > m { m = c }
    return m
}

ILEmitterTests3__Min_Of_Three_Values

core runnable verified

Runs `min3` → 2

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Min_Of_Three_Values   topic: core   status: verified
// verified behavior: Test.min3(...) == 2

namespace Test

func min3(a: int, b: int, c: int) -> int {
    var m = a
    if b < m { m = b }
    if c < m { m = c }
    return m
}

Runs `test` → 36

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Nested_For_In_Range_Sum   topic: core   status: verified
// verified behavior: Test.test(...) == 36

namespace Test

func test() -> int {
    var total = 0
    for i in 1..4 {
        for j in 1..4 {
            total = total + i * j
        }
    }
    return total
}

Runs `test` → 19

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Nested_Function_Calls_Three_Deep   topic: core   status: verified
// verified behavior: Test.test(...) == 19

namespace Test

func a(x: int) -> int = x + 1
func b(x: int) -> int = a(x) * 2
func c(x: int) -> int = b(x) - 3

func test() -> int = c(10)

Runs `test` → 36

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Nested_While_Sum_Multiplication_Table   topic: core   status: verified
// verified behavior: Test.test(...) == 36

namespace Test

func test() -> int {
    var total = 0
    var i = 1
    while i <= 3 {
        var j = 1
        while j <= 3 {
            total = total + i * j
            j = j + 1
        }
        i = i + 1
    }
    return total
}

ILEmitterTests3__Parity_Check

core runnable verified

Runs `parity` → "even"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Parity_Check   topic: core   status: verified
// verified behavior: Test.parity(...) == "even"

namespace Test

func parity(n: int) -> string {
    if n % 2 == 0 {
        return "even"
    }
    return "odd"
}

ILEmitterTests3__Power_Recursive

core runnable verified

Runs `pow` → 125

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Power_Recursive   topic: core   status: verified
// verified behavior: Test.pow(...) == 125

namespace Test

func pow(base: int, exp: int) -> int {
    if exp == 0 {
        return 1
    }
    return base * pow(base, exp - 1)
}

ILEmitterTests3__Product_Of_Range

core runnable verified

Runs `product` → 720

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Product_Of_Range   topic: core   status: verified
// verified behavior: Test.product(...) == 720

namespace Test

func product(lo: int, hi: int) -> int {
    var p = 1
    for i in lo..hi {
        p = p * i
    }
    return p
}

ILEmitterTests3__Range_Of_Squares

core runnable verified

Runs `test` → 285

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Range_Of_Squares   topic: core   status: verified
// verified behavior: Test.test(...) == 285

namespace Test

func test(n: int) -> int {
    var sum = 0
    for i in 1..n {
        sum = sum + i * i
    }
    return sum
}

Runs `sum_down` → 5050

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Recursion_Sum_Down_To_Zero   topic: core   status: verified
// verified behavior: Test.sum_down(...) == 5050

namespace Test

func sum_down(n: int) -> int {
    if n == 0 {
        return 0
    }
    return n + sum_down(n - 1)
}

Runs `test` → 15

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::RefData_Field_Mutation_Persists   topic: core   status: verified
// verified behavior: Test.test(...) == 15

namespace Test

class Bag {
    var capacity: int
    init(c: int) { self.capacity = c }
}

func test() -> int {
    let b = Bag(0)
    b.capacity = 10
    b.capacity = b.capacity + 5
    return b.capacity
}

Runs `test` → 42

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::RefData_Init_Stores_Param_To_Field   topic: core   status: verified
// verified behavior: Test.test(...) == 42

namespace Test

class Box {
    pub n: int
    init(value: int) { self.n = value }
}

func test() -> int {
    let b = Box(42)
    return b.n
}

Runs `test` → 3

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::RefData_Instance_Method_Accesses_Self_Field   topic: core   status: verified
// verified behavior: Test.test(...) == 3

namespace Test

class Counter {
    var n: int
    init() { self.n = 0 }

    func bump() {
        self.n = self.n + 1
    }

    func value() -> int = self.n
}

func test() -> int {
    let c = Counter()
    c.bump()
    c.bump()
    c.bump()
    return c.value()
}

Runs `test` → 321

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::RefData_Multi_Field_Construction   topic: core   status: verified
// verified behavior: Test.test(...) == 321

namespace Test

class Point3 {
    pub x: int
    pub y: int
    pub z: int
    init(a: int, b: int, c: int) {
        self.x = a
        self.y = b
        self.z = c
    }
}

func test() -> int {
    let p = Point3(1, 2, 3)
    return p.x + p.y * 10 + p.z * 100
}

Runs `rev` → 0

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Reverse_Digits_Recursive_Helper_Pin   topic: core   status: verified
// verified behavior: Test.rev(...) == 0

namespace Test

func rev_helper(n: int, acc: int) -> int {
    if n == 0 {
        return acc
    }
    return rev_helper(n / 10, acc * 10 + n % 10)
}

func rev(n: int) -> int = rev_helper(n, 0)

Runs `test` → 18

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Skip_Logic_With_Continue   topic: core   status: verified
// verified behavior: Test.test(...) == 18

namespace Test

func test() -> int {
    var collected = 0
    var i = 0
    while i < 20 {
        i = i + 1
        if i == 7 {
            continue
        }
        if i == 13 {
            continue
        }
        collected = collected + 1
    }
    return collected
}

Runs `test` → 0

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::String_Empty_Length_Zero   topic: core   status: verified
// verified behavior: Test.test(...) == 0

namespace Test

func test() -> int = "".Length

Runs `test` → false

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::String_Length_Comparison   topic: core   status: verified
// verified behavior: Test.test(...) == false

namespace Test

func test(s: string) -> bool = s.Length > 3

Runs `test` → 2450

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Sum_Even_Numbers_Below_N   topic: core   status: verified
// verified behavior: Test.test(...) == 2450

namespace Test

func test(n: int) -> int {
    var total = 0
    for i in 0..n {
        if i % 2 == 0 {
            total = total + i
        }
    }
    return total
}

ILEmitterTests3__Sum_From_1_To_100

core runnable verified

Runs `test` → 5050

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Sum_From_1_To_100   topic: core   status: verified
// verified behavior: Test.test(...) == 5050

namespace Test

func test() -> int {
    var total = 0
    for i in 1..101 {
        total = total + i
    }
    return total
}

Runs `dsum` → 45

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Sum_Of_Digits_Recursive   topic: core   status: verified
// verified behavior: Test.dsum(...) == 45

namespace Test

func dsum(n: int) -> int {
    if n == 0 {
        return 0
    }
    return n % 10 + dsum(n / 10)
}

Runs `test` → 2010

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Swap_Pattern_With_Temp   topic: core   status: verified
// verified behavior: Test.test(...) == 2010

namespace Test

func test() -> int {
    var a = 10
    var b = 20
    let tmp = a
    a = b
    b = tmp
    return a * 100 + b
}

ILEmitterTests3__Ternary_Basic

core runnable verified

Runs `test` → "neg"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Ternary_Basic   topic: core   status: verified
// verified behavior: Test.test(...) == "neg"

namespace Test

func test(x: int) -> string = x > 0 ? "pos" : "neg"

ILEmitterTests3__Ternary_Nested

core runnable verified

Runs `sign` → 0

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Ternary_Nested   topic: core   status: verified
// verified behavior: Test.sign(...) == 0

namespace Test

func sign(n: int) -> int = n > 0 ? 1 : n < 0 ? -1 : 0

Runs `abs` → 7

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Ternary_With_Int_Return   topic: core   status: verified
// verified behavior: Test.abs(...) == 7

namespace Test

func abs(n: int) -> int = n > 0 ? n : 0 - n

ILEmitterTests3__Triangle_Number

core runnable verified

Runs `tri` → 5050

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Triangle_Number   topic: core   status: verified
// verified behavior: Test.tri(...) == 5050

namespace Test

func tri(n: int) -> int = n * (n + 1) / 2

Runs `test` → 321

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Tuple_Let_Destructure_Three_Element   topic: core   status: verified
// verified behavior: Test.test(...) == 321

namespace Test

func triple() -> (int, int, int) = (1, 2, 3)

func test() -> int {
    let (a, b, c) = triple()
    return a + b * 10 + c * 100
}

Runs `test` → 73

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Tuple_Let_Destructure_Two_Element   topic: core   status: verified
// verified behavior: Test.test(...) == 73

namespace Test

func swap(a: int, b: int) -> (int, int) = (b, a)

func test() -> int {
    let (x, y) = swap(3, 7)
    return x * 10 + y
}

ILEmitterTests3__Tuple_Mixed_Types

core runnable verified

Runs `test` → "yes:42"

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Tuple_Mixed_Types   topic: core   status: verified
// verified behavior: Test.test(...) == "yes:42"

namespace Test

func mixed() -> (int, string, bool) = (42, "yes", true)

func test() -> string {
    let (n, s, b) = mixed()
    return s + ":" + n.ToString()
}

Runs `test` → 100

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Variable_Read_In_Inner_Block   topic: core   status: verified
// verified behavior: Test.test(...) == 100

namespace Test

func test() -> int {
    let outer = 100
    var x = 0
    if outer > 50 {
        x = outer
    }
    return x
}

Runs `test` → 0

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::Void_Returning_Function_Compiles_And_Runs   topic: core   status: verified
// verified behavior: Test.test(...) == 0

namespace Test

func noop() {
    let x = 1
}

func test() -> int {
    noop()
    return 0
}

Runs `test` → 5

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::While_With_Break_Exits_Early   topic: core   status: verified
// verified behavior: Test.test(...) == 5

namespace Test

func test() -> int {
    var i = 0
    while true {
        if i >= 5 {
            break
        }
        i = i + 1
    }
    return i
}

Runs `test` → 25

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests3.cs::While_With_Continue_Skips_Iteration   topic: core   status: verified
// verified behavior: Test.test(...) == 25

namespace Test

func test() -> int {
    var sum = 0
    var i = 0
    while i < 10 {
        i = i + 1
        if i % 2 == 0 {
            continue
        }
        sum = sum + i
    }
    return sum
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: MatchTypePatternTests.cs::AsBang_AssertingCast_Unbox   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test
func mustBeInt(boxed: object) -> int = boxed as! int
func go() -> int {
    let o: object = 42
    return mustBeInt(o)
}

Compiles

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: MixedLanguageTests.cs::Esharp_Func_Called_From_Csharp   topic: core   status: verified
// compiles cleanly (no auto-run claim was extracted)

namespace Test

func triple(n: int) -> int = n * 3