Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fold? は有用か #1163

Open
Seasawher opened this issue Nov 26, 2024 · 1 comment
Open

fold? は有用か #1163

Seasawher opened this issue Nov 26, 2024 · 1 comment

Comments

@Seasawher
Copy link
Member

そもそもfoldとOptionの組み合わせで書けるのか?

@Seasawher
Copy link
Member Author

Seasawher commented Nov 27, 2024

実装するとしたらこういう感じ

head? を取ってきて init に渡せば foldrM とかで書けるような気もする

universe u v
variable {α : Type u} {β : Type v}

def List.foldr? (f : α → α → α) : List α → Option α
  | [] => none
  | a :: as =>
    match foldr? f as with
    | none => some a
    | some b => some (f a b)

#guard [13, 12, 3, 6].foldr? (min · ·) = some 3
#guard [1, 2, 3, 4].foldr? (· + ·) = some 10
#guard ([] : List Nat).foldr? (max · ·) = none

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant