Why does allodd [] in functional language get True?

Source: Internet
Author: User
0 reply content:

List().forall(f)
I think Vacuous truth mentioned in other answers I have answered this question very easily.

True && x  -- => xx && True  -- => x
"All elements are odd data" and so on. "No element is not odd data 」.
No element exists in an empty set. Naturally, there is no "element not an odd number 」. Therefore, no element in the empty set is an odd number, that is, all elements in the empty set are odd numbers.

If all the elements in a set are odd data, then all the elements in their subsets should be odd data.
{1} is an odd data set, so its subset {} should also be a unique data set 」.

First time to see Vacuous truth Those who read this idea will often feel unreasonable and understandable. However, if I take an exam, I will find that this is the only rational solution. @ Zu and ZHAN said in the "unit yuan.

Empty product on Wikipedia This article is well explained. Set x1 x2 to two arrays,

According to common sense, there should be odd (x1: x2) = odd (x1) & odd (x2)

Obtain odd ([]) = true from the perspective of program implementation by taking x1 = [] x2 as a non-null value, the simplest logic for meeting all requirements is to return non-(loop judgment) If there is one that does not meet the requirements. In the end, the default situation is true, similar to a fast failure policy, so the result of a null set is true. The pseudocode is as follows:

for x in xs  if x is not odd    return falsereturn true
If you use recursive writing to determine whether an array is odd or not


Head (x) x removes the last element
Last element of Tail (x) x

Odd (x) = odd (head (x) & (tail (x) % 2 = 1) [Sorry, I forgot the syntax ..

So when x has only one element
Odd (empty set) & (tail (x) % 2 = 1)


In this case, the odd (empty set) can only be expressed by trueall f xs as follows:
All elements in xs meet the condition f.
That is:
Xs does not contain elements that do not meet condition f.

Therefore, the element of... does not exist in []. Of course, it is True.

In terms of semantics, @ Geek. cs has already been quite clear, and the Implementation Details @ zu and Zhan have been quite clear,

So if you design a standard library and a general all function, it will not look like another.

Of course, if you need special processing of empty sets in specific application scenarios, You can package a special all for your own use. For example, you can return Nothing when accepting, of course, the return value type is Maybe. The implementation of the standard library can achieve logical self-consistency without returning Maybe. Naturally, it will not choose to implement something that is so troublesome to use. Short ver: What do you think should you return? False? Error? Maybe Bool? In mathematics, it is called vacuous truth: Vacuous truth. I used it a long time ago in topology class.

This is quite intuitive. Why do the subject think it is against humanity. Just like in the wiki example, if a person asks, "is all the mobile phones in this room turned off ?" If no one in the room has a cell phone, normal people will not answer the question.

Perhaps we can better understand the inverse negative proposition: there are no even numbers in the array.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.