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

all isDigit for empty String should return False #621

Closed
galderz opened this Issue May 23, 2016 · 6 comments

Comments

Projects
None yet
3 participants
@galderz

galderz commented May 23, 2016

Hi, I'm playing around with Elm 0.17 Repl and I've noticed this:

> String.all Char.isDigit ""
True : Bool

That can't be right, empty String as no digits, it should return False, shouldn't it?

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender May 23, 2016

Contributor

All characters in the empty string are digits. Just as all numbers in the empty list are positive. And all eggs in an empty bag are blue.

There is no error here.

Contributor

jvoigtlaender commented May 23, 2016

All characters in the empty string are digits. Just as all numbers in the empty list are positive. And all eggs in an empty bag are blue.

There is no error here.

@galderz

This comment has been minimized.

Show comment
Hide comment
@galderz

galderz May 23, 2016

Hmmmm, still does not make sense to me. If you don't have anything within a String or List, I can't see how you can make such a truthful statement about the entirety of it.

galderz commented May 23, 2016

Hmmmm, still does not make sense to me. If you don't have anything within a String or List, I can't see how you can make such a truthful statement about the entirety of it.

@galderz

This comment has been minimized.

Show comment
Hide comment
@galderz

galderz May 23, 2016

Any links to better explanations?

galderz commented May 23, 2016

Any links to better explanations?

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender May 23, 2016

Contributor

This is not an Elm or even programming question. It's basic mathematical logic. For example, you may want to read this: http://math.stackexchange.com/questions/202452/why-is-predicate-all-as-in-allset-true-if-the-set-is-empty

But it all really boils down to this:

  • I show you an empty bag.
  • I ask you: "Are all eggs in this bag blue?"
  • You should answer "Yes".
  • But if you answer "No", then I will ask you: "So, you think not all eggs in this bag are blue? Can you please show me at least one egg in this bag which is not blue?"
  • You won't be able to show me a not-blue egg in that bag, so you will have to concede that indeed, all the eggs that are in that bag (which happen to be exactly zero, but that doesn't matter, it's still all of them) are blue.

Of course, at the same time it is also true that all eggs in that empty bag are red. And that's not a contradiction. It's how mathematical logic, and indeed natural language logic as well, works.

Other than the above explanation, you can certainly google dozens of equivalent explanations for other programming languages. For example, https://issues.scala-lang.org/browse/SI-8587

Contributor

jvoigtlaender commented May 23, 2016

This is not an Elm or even programming question. It's basic mathematical logic. For example, you may want to read this: http://math.stackexchange.com/questions/202452/why-is-predicate-all-as-in-allset-true-if-the-set-is-empty

But it all really boils down to this:

  • I show you an empty bag.
  • I ask you: "Are all eggs in this bag blue?"
  • You should answer "Yes".
  • But if you answer "No", then I will ask you: "So, you think not all eggs in this bag are blue? Can you please show me at least one egg in this bag which is not blue?"
  • You won't be able to show me a not-blue egg in that bag, so you will have to concede that indeed, all the eggs that are in that bag (which happen to be exactly zero, but that doesn't matter, it's still all of them) are blue.

Of course, at the same time it is also true that all eggs in that empty bag are red. And that's not a contradiction. It's how mathematical logic, and indeed natural language logic as well, works.

Other than the above explanation, you can certainly google dozens of equivalent explanations for other programming languages. For example, https://issues.scala-lang.org/browse/SI-8587

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender May 23, 2016

Contributor

Maybe this will help you most: https://en.wikipedia.org/wiki/Vacuous_truth

Contributor

jvoigtlaender commented May 23, 2016

Maybe this will help you most: https://en.wikipedia.org/wiki/Vacuous_truth

@jarvismartin

This comment has been minimized.

Show comment
Hide comment
@jarvismartin

jarvismartin Mar 12, 2018

TL;DR: Explicit checks for empty strings, errors, etc. yield more predictable results.
This is probably helpful: http://package.elm-lang.org/packages/elm-lang/core/latest/String#toInt
The important thing to notice is that if you're checking user input, you want to use Return.withDefault.
It provides a default that catches the error state you're looking for, without too much plumbing.

jarvismartin commented Mar 12, 2018

TL;DR: Explicit checks for empty strings, errors, etc. yield more predictable results.
This is probably helpful: http://package.elm-lang.org/packages/elm-lang/core/latest/String#toInt
The important thing to notice is that if you're checking user input, you want to use Return.withDefault.
It provides a default that catches the error state you're looking for, without too much plumbing.

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