Skip to content

core — examples

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

Runs `test` → false

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

namespace Test

func test(a: bool, b: bool) -> bool = a && b

Runs `test` → false

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

namespace Test

func test(a: bool, b: bool) -> bool = a || b

ILEmitterTests2__Min_Max_Pin

core runnable verified

Runs `minimum` → 5

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

namespace Test

func minimum(a: int, b: int) -> int {
    if a < b {
        return a
    }
    return b
}

func maximum(a: int, b: int) -> int {
    if a > b {
        return a
    }
    return b
}

ILEmitterTests2__Modulo_Zero_Result

core runnable verified ×2

Runs `test` → 0

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

namespace Test

func test(a: int, b: int) -> int = a % b

Runs `mul` → 0

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

namespace Test

func mul(a: int, b: int) -> int {
    var result = 0
    var i = 0
    while i < b {
        result = result + a
        i = i + 1
    }
    return result
}

Runs `test` → -42

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

namespace Test

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

Runs `test` → false

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

namespace Test

func test(n: int) -> bool = n < -10

Runs `pow2` → 65536

// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: ILEmitterTests2.cs::Power_Of_Two_Via_While_Pin   topic: core   status: verified
// verified behavior: Test.pow2(...) == 65536

namespace Test

func pow2(n: int) -> int {
    var result = 1
    var i = 0
    while i < n {
        result = result * 2
        i = i + 1
    }
    return result
}

ILEmitterTests2__Range_Check_With_And

core runnable verified

Runs `in_range` → false

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

namespace Test

func in_range(n: int) -> bool = n >= 0 && n <= 100

Runs `is_odd` → false

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

namespace Test

func is_even(n: int) -> bool {
    if n == 0 {
        return true
    }
    return is_odd(n - 1)
}

func is_odd(n: int) -> bool {
    if n == 0 {
        return false
    }
    return is_even(n - 1)
}

Runs `test` → false

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

namespace Test

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

func test() -> bool {
    let a = Box(5)
    let b = Box(5)
    return a == b
}

Runs `test` → true

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

namespace Test

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

func test() -> bool {
    let a = Box(5)
    let b = a
    return a == b
}

ILEmitterTests2__Reverse_Digits_Pin

core runnable verified

Runs `reverse` → 0

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

namespace Test

func reverse(n: int) -> int {
    var result = 0
    var x = n
    while x > 0 {
        result = result * 10 + x % 10
        x = x / 10
    }
    return result
}

Runs `test` → "count=42"

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

namespace Test

func test(label: string, n: int) -> string = label + n.ToString()

Runs `test` → "hello world"

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

namespace Test

func test(a: string, b: string) -> string = a + b

Runs `test` → false

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

namespace Test

func test(a: string, b: string) -> bool = a == b

Runs `test` → 11

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

namespace Test

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

Runs `test` → 42

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

namespace Test

func make() -> (int, string) = (42, "hi")

func test() -> int {
    let t = make()
    return t.Item1
}

Runs `test` → "hi"

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

namespace Test

func make() -> (int, string) = (42, "hi")

func test() -> string {
    let t = make()
    return t.Item2
}

ILEmitterTests2__Tuple_Three_Element

core runnable verified

Runs `test` → 6

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

namespace Test

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

func test() -> int {
    let t = make()
    return t.Item1 + t.Item2 + t.Item3
}

Runs `test` → 0

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

namespace Test

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

Runs `test` → 0

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

namespace Test

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

ILEmitterTests2__While_Sum_To_N_Idiom

core runnable verified

Runs `test` → 0

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

namespace Test

func test(n: int) -> int {
    var total = 0
    var i = 1
    while i <= n {
        total = total + i
        i = i + 1
    }
    return total
}

ILEmitterTests3__Ackermann_Small_Pin

core runnable verified

Runs `ack` → 7

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

namespace Test

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

Runs `test` → 7

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

namespace Test

func test() -> int {
    let seven = () => 7
    return seven()
}

ILEmitterTests3__Average_Of_Three

core runnable verified

Runs `avg` → 10

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

namespace Test

func avg(a: int, b: int, c: int) -> int = (a + b + c) / 3

Runs `find_in_sorted` → -1

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

namespace Test

func find_in_sorted(target: int, n: int) -> int {
    var lo = 0
    var hi = n
    while lo < hi {
        let mid = (lo + hi) / 2
        if mid == target {
            return mid
        }
        if mid < target {
            lo = mid + 1
        }
        if mid > target {
            hi = mid
        }
    }
    return -1
}

Runs `test` → "not pos"

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

namespace Test

func is_positive(n: int) -> bool = n > 0

func test(n: int) -> string {
    if is_positive(n) {
        return "pos"
    }
    return "not pos"
}

Runs `test` → 15

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

namespace Test

func test() -> int {
    var sum = 0
    let add = func(x: int) { sum = sum + x }
    for i in 1..6 {
        add(i)
    }
    return sum
}

Runs `test` → 220

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

namespace Test

func test() -> int {
    let base_val = 100
    let with_base = func(x: int) -> int = base_val + x
    return with_base(5) + with_base(15)
}

Runs `test` → 3

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

namespace Test

func test() -> int {
    var total = 0
    let inc = func() { total = total + 1 }
    inc()
    inc()
    inc()
    return total
}

Runs `test` → false

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

namespace Test

func test(a: int, b: int) -> bool = a * 2 > b + 5

Runs `test` → 25

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

namespace Test

func test() -> int {
    var x = 100
    x /= 4
    return x
}

Runs `test` → 55

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

namespace Test

func test(n: int) -> int {
    var total = 0
    var i = 0
    while i < n {
        total += i + 1
        i += 1
    }
    return total
}

Runs `test` → 70

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

namespace Test

func test() -> int {
    var x = 100
    x -= 30
    return x
}

Runs `test` → 12

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

namespace Test

func test() -> int {
    var x = 3
    x *= 4
    return x
}

Runs `test` → 18

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

namespace Test

func test() -> int {
    var x = 10
    x += 5
    x += 3
    return x
}

Runs `test` → 10

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

namespace Test

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

Runs `count_digits` → 6

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

namespace Test

func count_digits(n: int) -> int {
    if n == 0 { return 1 }
    var x = n
    var count = 0
    while x > 0 {
        count = count + 1
        x = x / 10
    }
    return count
}

Compiles

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

namespace Test

func test() -> int {
    var x = 0
    defer { x = 1 }
    return x
}

Runs `first_above` → 101

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

namespace Test

func first_above(threshold: int) -> int {
    var i = 0
    while i < 1000 {
        if i > threshold {
            break
        }
        i = i + 1
    }
    return i
}

Runs `grade` → "F"

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

namespace Test

func grade(s: int) -> string {
    if s >= 90 { return "A" }
    else if s >= 80 { return "B" }
    else if s >= 70 { return "C" }
    else if s >= 60 { return "D" }
    else { return "F" }
}

Runs `test` → "zero"

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

namespace Test

func test(x: int) -> string {
    if x > 0 {
        return "pos"
    } else if x < 0 {
        return "neg"
    } else {
        return "zero"
    }
}

ILEmitterTests3__Float_Division_Real

core runnable verified

Runs `test` → 3.5

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

namespace Test

func test() -> double = 7.0 / 2.0

Runs `test` → 100

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

namespace Test

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

Runs `test` → 35

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

namespace Test

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

Runs `test` → 45

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

namespace Test

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

Runs `test` → 5

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

namespace Test

func test() -> int {
    var sum = 0
    for i in 5..6 {
        sum = sum + i
    }
    return sum
}

Runs `test` → 25

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

namespace Test

func sq(x: int) -> int = x * x

func test() -> int {
    return sq(3) + sq(4)
}

Runs `test` → 42

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

namespace Test

func double_it(x: int) -> int = x * 2
func increment(x: int) -> int = x + 1

func test() -> int {
    return double_it(increment(20))
}