lean-ja / lean-ja/lean-by-example
List α の要素を Std.Range を使って範囲取得する
Open
Nobody has claimed this yet.
メモ
- Dominant language
- Lean
- Stars
- 188
- Forks
- 15
- Avg merge
- 9h 8m
- Merged PRs (30d)
- 6
Description
まずは関数として作ってみようということで、これが試作品
疑問:
- 指定された Range が List.length からはみ出ているときに、「取れるだけ取る」のか、それともエラーや none として返すのかで方針が分かれうることに気が付いた。
universe u
def List.getRange {α : Type u} (l : List α) (range : Std.Range)
(valid : range.stop < l.length := by get_elem_tactic) : List α := Id.run do
let mut result := []
for h : i in range do
result := (
have : i < l.length := by
dsimp [Membership.mem] at h
omega
l.get ⟨i, this⟩ :: result
)
return result.reverse
def List.getRange! {α : Type u} [Inhabited α] (l : List α) (range : Std.Range) : List α := Id.run do
let mut result := []
for i in range do
result := l.get! i :: result
return result.reverse
def List.getRange? {α : Type u} (l : List α) (range : Std.Range) : Option (List α) := do
let mut result := []
for i in range do
let a ← l.get? i
result := a :: result
return result.reverse
#guard [1, 2, 3, 4, 5].getRange [0 : 2] = [1, 2]
#guard [1, 2, 3, 4, 5].getRange! [0 : 4] = [1, 2, 3, 4]
#guard [1, 2, 3, 4, 5].getRange? [0 : 4] = some [1, 2, 3, 4]
#guard [1, 2, 3, 4, 5].getRange? [2 : 6] = none
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start from the prototype definitions and #guard examples in the issue, focusing on how the proposed getRange, getRange!, and getRange? variants handle ranges beyond List.length. Resolve whether out-of-bounds ranges should truncate, fail, or return none, then document the decision with corresponding examples and tests.
Written by the indexing model from the issue text.
Assessment
- Domain
- api
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100