Skip to content

core — examples

← all topics · 500 examples · page 8 of 10 · raw source ↓

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))
}

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))
}

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` → 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
}