Skip to content

core — examples

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

Runs `go` → 3

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

func go() -> int {
  var n = 0
  var s = "go"
  while s == "go" {
    n += 1
    if n >= 3 { s = "stop" }
  }
  return n
}

Runs `go` → false

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

func go() -> bool {
  let a = "x"
  let b = "x" + ""
  return a != b
}

Runs `go` → true

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

func go() -> bool {
  let a = "x"
  let b = "y"
  return a != b
}

Runs `go` → false

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

func eq(s: string) -> bool = s == "hello"
func go() -> bool = eq("world")

Runs `go` → true

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

func eq(s: string) -> bool = s == "hello"
func go() -> bool = eq("hello")

Runs `go` → true

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

func go() -> bool {
  let s = "hello"
  let h = s.Substring(0, 3)
  return h == "hel"
}

Runs `go` → true

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

func go() -> bool {
  let a = "foo" + "bar"
  let b = "foob" + "ar"
  return a == b
}

Runs `go` → 11

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

namespace Test

func go(task: int) -> int { return task + 1 }

ILEmitterTests2__Absolute_Value_Pin

core runnable verified

Runs `abs` → 0

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

namespace Test

func abs(n: int) -> int {
    if n < 0 {
        return -n
    }
    return n
}

Runs `test` → 30

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

namespace Test

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

Runs `choose` → 200

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

namespace Test

func choose(flag: bool) -> int {
    if flag {
        return 100
    }
    return 200
}

Runs `test` → 6

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

namespace Test

func test() -> int {
    var outer = 0
    var i = 0
    while i < 3 {
        var j = 0
        while j < 10 {
            if j == 2 {
                break
            }
            outer = outer + 1
            j = j + 1
        }
        i = i + 1
    }
    return outer
}

ILEmitterTests2__Clamp_Idiom_Pin

core runnable verified

Runs `clamp` → 7

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

namespace Test

func clamp(n: int, lo: int, hi: int) -> int {
    if n < lo {
        return lo
    }
    if n > hi {
        return hi
    }
    return n
}

ILEmitterTests2__Collatz_Length_Pin

core runnable verified

Runs `collatz_steps` → 8

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

namespace Test

func collatz_steps(n: int) -> int {
    var x = n
    var steps = 0
    while x > 1 {
        if x % 2 == 0 {
            x = x / 2
        } else {
            x = x * 3 + 1
        }
        steps = steps + 1
    }
    return steps
}

Runs `test` → 101010

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

namespace Test

func test(a: int, b: int) -> int {
    var result = 0
    if a == b { result = result + 1 }
    if a != b { result = result + 10 }
    if a < b  { result = result + 100 }
    if a > b  { result = result + 1000 }
    if a <= b { result = result + 10000 }
    if a >= b { result = result + 100000 }
    return result
}

Runs `describe` → "grade: F"

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

namespace Test

func describe(score: int) -> string {
    var label = ""
    if score >= 90 {
        label = "A"
    }
    if score >= 80 && score < 90 {
        label = "B"
    }
    if score >= 70 && score < 80 {
        label = "C"
    }
    if score < 70 {
        label = "F"
    }
    return "grade: " + label
}

Runs `test` → 9

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

namespace Test

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

ILEmitterTests2__Count_Set_Bits_Pin

core runnable verified

Runs `popcount` → 1

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

namespace Test

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

ILEmitterTests2__Defer_LIFO_Ordering

core runnable verified

Runs `test` → "X"

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

namespace Test

func test() -> string {
    var s = ""
    defer { s = s + "C" }
    defer { s = s + "B" }
    defer { s = s + "A" }
    s = s + "X"
    return s
}

Runs `test` → ".early"

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

namespace Test

func test() -> string {
    var trace = ""
    defer { trace = trace + "D" }
    if true {
        return trace + ".early"
    }
    return trace
}

Runs `digit_sum` → 1

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

namespace Test

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

Runs `fact` → 3628800

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

namespace Test

func fact(n: int) -> int {
    if n <= 1 {
        return 1
    }
    return n * fact(n - 1)
}

Runs `fib` → 6765

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

namespace Test

func fib(n: int) -> int {
    if n < 2 {
        return n
    }
    var a = 0
    var b = 1
    var i = 2
    while i <= n {
        let next = a + b
        a = b
        b = next
        i = i + 1
    }
    return b
}

Runs `fib` → 55

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

namespace Test

func fib(n: int) -> int {
    if n < 2 {
        return n
    }
    return fib(n - 1) + fib(n - 2)
}

ILEmitterTests2__Float_Add_Double

core runnable verified

Runs `test` → 3.5

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

namespace Test

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

ILEmitterTests2__Float_Div_Double

core runnable verified

Runs `test` → 2.5

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

namespace Test

func test(a: double, b: double) -> double = a / b

ILEmitterTests2__Float_Mul_Double

core runnable verified

Runs `test` → 6.0

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

namespace Test

func test(a: double, b: double) -> double = a * b

ILEmitterTests2__Float_Sub_Double

core runnable verified

Runs `test` → 0.75

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

namespace Test

func test(a: double, b: double) -> double = a - b

Runs `test` → 10

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

namespace Test

func test() -> int {
    let x = 10
    let read = func() -> int = x
    return read()
}

Runs `sign` → 0

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

namespace Test

func sign(n: int) -> int {
    if n > 0 { return 1 }
    if n < 0 { return -1 }
    return 0
}

Runs `is_even` → true

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

namespace Test

func is_even(n: int) -> bool = n % 2 == 0

ILEmitterTests2__GCD_Euclidean_Pin

core runnable verified

Runs `gcd` → 7

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

namespace Test

func gcd(a: int, b: int) -> int {
    var x = a
    var y = b
    while y != 0 {
        let t = y
        y = x % y
        x = t
    }
    return x
}

Runs `sum_squares` → 385

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

namespace Test

func square(n: int) -> int = n * n

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

Runs `test` → "zero"

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

namespace Test

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

ILEmitterTests2__If_Mutates_Outer_Var

core runnable verified

Runs `test` → 0

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

namespace Test

func test(n: int) -> int {
    var result = 0
    if n > 0 {
        result = 1
    }
    if n < 0 {
        result = -1
    }
    return result
}

Runs `isqrt` → 31

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

namespace Test

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

Runs `is_prime` → true

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

namespace Test

func is_prime(n: int) -> bool {
    if n < 2 {
        return false
    }
    if n == 2 {
        return true
    }
    if n % 2 == 0 {
        return false
    }
    var d = 3
    while d * d <= n {
        if n % d == 0 {
            return false
        }
        d = d + 2
    }
    return true
}

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

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