Skip to content

First ​

The first element of a collection.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageFirst!
First(collection)the first element of the collection.
First(collection, default)default when the collection is empty, instead of the first element.
First(expr)the first operand of any expression, not just a collection.
Typed signatures
TypePackageApplies
(xs: indexed_collection<any>) -> anycompute-enginealways
(any, any?) -> anycombinatoricsreplaces compute-enginealways

Domain: Collections

Details
  • First(c)=At(c,1)First(c) = At(c, 1). See At.
  • Positional indexing from the front (index 1) and back (negative indices).
  • On an empty collection with no default given, returns the symbol MissingMissing rather than raising an error.
  • A second argument supplies a default for an empty collection, instead of MissingMissing.
  • Works on the operands of any expression, not just a collection's elements.

Examples

4 more kept as data, checked against the oracles
Basic #
live ↗
InFirst([1,2,3])\mathrm{First}(\bigl\lbrack1, 2, 3\bigr\rbrack)Out11
live ↗
InFirst([10,20,30])\mathrm{First}(\bigl\lbrack10, 20, 30\bigr\rbrack)Out1010
live ↗
InFirst([a,b,c])\mathrm{First}(\bigl\lbrack a, b, c\bigr\rbrack)Outaa

An empty list yields the supplied default instead of failing

live ↗
InFirst([],x)\mathrm{First}(\bigl\lbrack\bigr\rbrack, x)Outxx
Scope #

A second argument supplies a default for an empty collection

live ↗
InFirst([],99)\mathrm{First}(\bigl\lbrack\bigr\rbrack, 99)Out9999

Of a matrix, the first row

live ↗
InFirst([[a,b],[c,d]])\mathrm{First}(\bigl\lbrack\bigl\lbrack a, b\bigr\rbrack, \bigl\lbrack c, d\bigr\rbrack\bigr\rbrack)Out[a,b]\bigl\lbrack a, b\bigr\rbrack

A lazy Range works too

live ↗
InFirst(5..10)\mathrm{First}(5..10)Out55

Any expression, not just a list: the first term of a sum

live ↗
InFirst(a2+b2)\mathrm{First}(a^2+b^2)Outa2a^2

A default is ignored when the collection has a first element

live ↗
InFirst([a,b],x)\mathrm{First}(\bigl\lbrack a, b\bigr\rbrack, x)Outaa
live ↗
InFirst(SparseArray(1..100))\mathrm{First}(\mathrm{SparseArray}(1..100))Out11
Properties #

First(c)=At(c,1)First(c) = At(c, 1). See At

live ↗
InFirst([1,2,3])=[1,2,3][1]\mathrm{First}(\bigl\lbrack1, 2, 3\bigr\rbrack)=\bigl\lbrack1, 2, 3\bigr\rbrack[1]OutTrue\mathrm{True}
Possible issues #

An empty collection has no first element; compute-engine returns the symbol MissingMissing

live ↗
InFirst([])\mathrm{First}(\bigl\lbrack\bigr\rbrack)OutMissing\mathrm{Missing}

A power is an expression with parts too: the first part of a−2a^{-2} is its base

live ↗
InFirst(1a2)\mathrm{First}(\frac{1}{a^2})Outaa
About
Examples17
Checked againstWolfram Language 16 agree1 differmathlib4 4 agree1 error
CrosswalkWolfram Language

Implementation

mappedmathlib4packages/oracle/src/mappings.ts

The head of a list; an empty list fails to elaborate (ours is Missing) rather than defaulting, and so does an expression that is not a list.

See also: Last, At