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

Provide a last function for lists #138

Closed
heueristik opened this issue Nov 6, 2024 · 5 comments
Closed

Provide a last function for lists #138

heueristik opened this issue Nov 6, 2024 · 5 comments

Comments

@heueristik
Copy link

There is a head function

head {A} (defaultValue : A) (list : List A) : A :=
  case list of
    | x :: _ := x
    | nil := defaultValue;

but no

last {A} (defaultValue : A) (list : List A) : A :=
  head@{
    defaultValue;
    list := reverse list;
  };
@mariari
Copy link
Member

mariari commented Nov 7, 2024

#139 fixes this issue

@lukaszcz
Copy link
Contributor

I don't think we should have last. There are few legitimate use-cases for it. It's most often used incorrectly by beginners when they try to use lists as if they were arrays.

@lukaszcz
Copy link
Contributor

lukaszcz commented Jan 9, 2025

We decided not to provide it, not in the Prelude at least.

@lukaszcz lukaszcz closed this as not planned Won't fix, can't repro, duplicate, stale Jan 9, 2025
@mariari
Copy link
Member

mariari commented Jan 11, 2025

Last comes up quite a bit with foldr patterns, it might be good to reconsider having it, as I'm not sure how else you'd give the first element to a foldr

@lukaszcz
Copy link
Contributor

Whever you want to take the last element and then do foldr, you can reverse the list, take the first element and do foldl. But it's true taking the last element might be more intuitive, so we might want to reconsider this decision.

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

Successfully merging a pull request may close this issue.

3 participants