This repository has been archived by the owner on Jan 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
64 additions
and
64 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,54 @@ | ||
/- | ||
# Problem 7 | ||
(Intermediate 🌟🌟) Flatten a nested list structure. | ||
-/ | ||
|
||
variable {α : Type} | ||
|
||
-- We have to define a new data type, because lists in Lean are homogeneous. | ||
inductive NestedList (α : Type) where | ||
| elem : α → NestedList α | ||
| list : List (NestedList α) → NestedList α | ||
|
||
/-- convert NestedList to String. -/ | ||
partial def NestedList.toString [ToString α] : NestedList α → String | ||
| NestedList.elem x => ToString.toString x | ||
| NestedList.list xs => "[" ++ String.intercalate ", " (xs.map toString) ++ "]" | ||
|
||
/-- Display `NestedList` in a readable manner when you run `#eval`. -/ | ||
instance [ToString α] : ToString (NestedList (α : Type)) where | ||
toString nl := NestedList.toString nl | ||
|
||
/-- flatten the list structure -/ | ||
def flatten (nl : NestedList α) : List α := | ||
-- sorry | ||
match nl with | ||
| .elem x => [x] | ||
| .list [] => [] | ||
| .list (x :: xs) => flatten x ++ flatten (.list xs) | ||
-- sorry | ||
|
||
-- The following codes are for test and you should not edit these. | ||
|
||
open NestedList | ||
|
||
def sample : NestedList Nat := | ||
list [elem 1, list [elem 2, elem 3], elem 4] | ||
|
||
#eval sample | ||
|
||
def empty : NestedList String := list [] | ||
|
||
#eval empty | ||
|
||
example : flatten (.elem 5) = [5] := by | ||
delta flatten | ||
rfl | ||
|
||
example : flatten sample = [1, 2, 3, 4] := by | ||
delta flatten | ||
rfl | ||
|
||
example : flatten (empty) = [] := by | ||
delta flatten | ||
rfl | ||
/- | ||
# Problem 7 | ||
(Intermediate 🌟🌟) Flatten a nested list structure. | ||
-/ | ||
|
||
variable {α : Type} | ||
|
||
-- We have to define a new data type, because lists in Lean are homogeneous. | ||
inductive NestedList (α : Type) where | ||
| elem : α → NestedList α | ||
| list : List (NestedList α) → NestedList α | ||
|
||
/-- convert NestedList to String. -/ | ||
partial def NestedList.toString [ToString α] : NestedList α → String | ||
| NestedList.elem x => ToString.toString x | ||
| NestedList.list xs => "[" ++ String.intercalate ", " (xs.map toString) ++ "]" | ||
|
||
/-- Display `NestedList` in a readable manner when you run `#eval`. -/ | ||
instance [ToString α] : ToString (NestedList (α : Type)) where | ||
toString nl := NestedList.toString nl | ||
|
||
/-- flatten the list structure -/ | ||
def flatten (nl : NestedList α) : List α := | ||
-- sorry | ||
match nl with | ||
| .elem x => [x] | ||
| .list [] => [] | ||
| .list (x :: xs) => flatten x ++ flatten (.list xs) | ||
-- sorry | ||
|
||
-- The following codes are for test and you should not edit these. | ||
|
||
open NestedList | ||
|
||
def sample : NestedList Nat := | ||
list [elem 1, list [elem 2, elem 3], elem 4] | ||
|
||
#eval sample | ||
|
||
def empty : NestedList String := list [] | ||
|
||
#eval empty | ||
|
||
example : flatten (.elem 5) = [5] := by | ||
delta flatten | ||
rfl | ||
|
||
example : flatten sample = [1, 2, 3, 4] := by | ||
delta flatten | ||
rfl | ||
|
||
example : flatten (empty) = [] := by | ||
delta flatten | ||
rfl |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters