Core: all isDigit for empty String should return False

Created on 23 May 2016  路  6Comments  路  Source: elm/core

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?

All 6 comments

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.

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.

Any links to better explanations?

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

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

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dullbananas picture dullbananas  路  3Comments

holmesmr picture holmesmr  路  6Comments

billstclair picture billstclair  路  8Comments

Spenhouet picture Spenhouet  路  5Comments

liamcurry picture liamcurry  路  4Comments