sum-empty-float
Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.
Returns 0.0, not 0, for an empty float list.
Syntax: <list<float>>.sum()
Category#
collection
Related#
- prima
- ultima
- list
Examples#
radix/corpus/lista/sum-empty-float.fab (canonical · existing-home)#
Returns 0.0, not 0, for an empty float list.
# =============================================================================
# sum — an empty float list sums to the float zero.
# =============================================================================
#
# What this teaches:
# • `sum()` is the fold that starts at the element type's additive identity,
# so an empty `list<float>` sums to the float zero `0.0` — never to the
# integer `0`, and never to a trap.
# • Empty input is not a special case at the call site: the same `sum()` works
# on an empty list, a populated list, and a list bound inside a function that
# returns the fold.
#
# Common mistakes:
# • Assuming an empty `sum()` has no answer and guarding it — it has one, and
# the guard hides the type the fold carries.
# • Assuming the empty result is the integer `0`; it is `0.0`, the float
# additive identity of the element type.
#
# See also: prima, ultima, list
# =============================================================================
# list<T>.sum() additive identity on the empty list
#
# e.sum() -- sum of the elements, element type's zero when empty
#
# GRAMMAR:
# listDecl :← ('let' | 'mut') 'list<' type '>' ident '←' expr
# listSum :← expr '.' 'sum' '(' ')'
#
# EXPECTED OUTPUT:
# 0.0
# 0.0
# true
# 4.0
# 0.0
#
# BACKEND: runner-pinned stdout (sum-empty-float.expected).
main {
let list<float> e ← empty
print e.sum()
let float z ← e.sum()
print z
print e.sum() ≡ 0.0
let list<float> f ← [1.5, 2.5]
print f.sum()
print sum_return()
}
fn sum_return() → float {
let list<float> none_yet ← empty
return none_yet.sum()
}Expected output:
0.0
0.0
true
4.0
0.0