core — examples
← all topics · 500 examples · page 9 of 10 · raw source ↓
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
}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"
}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)
}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
}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 `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 = "".LengthRuns `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 > 3Runs `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
}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
}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"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 : 0Runs `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 - nRuns `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) / 2Runs `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
}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: MixedLanguageTests.cs::CSharp_Instance_Method_Called_From_Esharp topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func describe(g: Greeter) -> string = g.Hello("world")Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: MixedLanguageTests.cs::CSharp_Static_Method_Called_From_Esharp topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func double_it(x: int) -> int = MathHelper.Twice(x)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 * 3Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: MixedLanguageTests.cs::Esharp_OutParam_BoundByCsharp_OutVar topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func try_parse(input: int, out doubled: int) -> bool {
doubled = input * 2
return true
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: OptionalArgTests.cs::BoolDefault_Used_When_Omitted topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> bool = Lib.flag()Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: OptionalArgTests.cs::CharDefault_Used_When_Omitted topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> string = Lib.repeat("ab")Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: OptionalArgTests.cs::DoubleDefault_Used_When_Omitted topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> double = Lib.half(8.0)Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: OptionalArgTests.cs::EnumDefault_Used_When_Omitted topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int = Lib.withMode(10)Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: OptionalArgTests.cs::IntDefault_Overridden_When_Passed topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int = Lib.scale(5, 3)Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: OptionalArgTests.cs::IntDefault_Used_When_Omitted topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int = Lib.scale(5)Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: OptionalArgTests.cs::LongDefault_Used_When_Omitted topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> long = Lib.big()Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: OptionalArgTests.cs::MultipleDefaults_AllOmitted topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int = Lib.two()Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: OptionalArgTests.cs::MultipleDefaults_FirstPassed_SecondDefaulted topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int = Lib.two(7)Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: OptionalArgTests.cs::NullReferenceDefault_Used_When_Omitted topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> int = Lib.lenOr()Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: OptionalArgTests.cs::StringDefault_Used_When_Omitted topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func go() -> string = Lib.label("hi")Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: PdbTests.cs::DebugSymbols_DllStillRunsCorrectly topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func add(a: int, b: int) -> int {
var result = a + b
return result
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: PdbTests.cs::NoDebugSymbols_NoPdbFile topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func run() -> int {
return 42
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: PdbTests.cs::Pdb_HasSequencePoints topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func compute(x: int) -> int {
var a = x + 1
var b = a * 2
return b
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: SourceSpanTests.cs::Binder_PropagatesSpanToStatements topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func run() -> int {
let x = 42
return x
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: SourceSpanTests.cs::IfStatement_HasCorrectSpan topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func check(x: int) -> int {
if x > 0 {
return x
}
return 0
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: SourceSpanTests.cs::Parser_CapturesStatementLineNumbers topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func add(a: int, b: int) -> int {
var total = a
total += b
return total
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TranspilerTests.cs::Transpiles_Attribute_On_Function topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
[HttpGet]
func getUsers() -> string {
return "users"
}Compiles
// E# — a verified example from the E# language corpus (CLR language; .es, not ECMAScript).
// provenance: TranspilerTests.cs::Transpiles_Closure_Captures_Let topic: core status: verified
// compiles cleanly (no auto-run claim was extracted)
namespace Test
func make_adder(base: int) -> int {
let offset = 10
let add = func(x: int) -> int { return offset + x }
return add(base)
}