[PATCH 1/3] Implement nand, nor, nany and nall in Data.List
Alexander Berntsen
alexander at plaimi.net
Thu Feb 27 15:13:00 UTC 2014
Implement negation-shorthands for `and`, `or`, `any` and `all` in
Data.List: `nand`, `nor`, `nany`, `nall`. These functions compose `not`
with `and`, `or`, `any` and `all` respectively.
The rationale is less typing, and a more readable and immediately
obvious way of doing this.
The added namespace pollution should not be dramatic. If someone has
defined the negations of `and` etc. elsewhere, they have likely defined
`and` etc. as well.
---
Data/List.hs | 4 ++++
GHC/List.lhs | 34 ++++++++++++++++++++++++++++++++--
changelog.md | 3 +++
3 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/Data/List.hs b/Data/List.hs
index 09aed9d..6675dfc 100644
--- a/Data/List.hs
+++ b/Data/List.hs
@@ -53,8 +53,12 @@ module Data.List
, concatMap
, and
, or
+ , nand
+ , nor
, any
, all
+ , nany
+ , nall
, sum
, product
, maximum
diff --git a/GHC/List.lhs b/GHC/List.lhs
index e004ded..a8b8950 100644
--- a/GHC/List.lhs
+++ b/GHC/List.lhs
@@ -25,8 +25,8 @@ module GHC.List (
foldl, scanl, scanl1, foldr, foldr1, scanr, scanr1,
iterate, repeat, replicate, cycle,
take, drop, splitAt, takeWhile, dropWhile, span, break,
- reverse, and, or,
- any, all, elem, notElem, lookup,
+ reverse, and, or, nand, nor,
+ any, all, nany, nall, elem, notElem, lookup,
concatMap,
zip, zip3, zipWith, zipWith3, unzip, unzip3,
errorEmptyList,
@@ -524,6 +524,22 @@ or (x:xs) = x || or xs
#-}
#endif
+-- | 'nand' returns the negated conjunction of a Boolean list. For the result
+-- to be 'False', the list must be finite; 'True', however, results from a
+-- 'False' value at a finite index of a finite or infinite list.
+--
+-- /Since: 4.7.0.0/
+nand :: [Bool] -> Bool
+nand = not . and
+
+-- | 'nor' returns the negated disjunction of a Boolean list. For the result
+-- to be 'True', the list must be finite; 'False', however, results from a
+-- 'True' value at a finite index of a finite or infinite list.
+--
+-- /Since: 4.7.0.0/
+nor :: [Bool] -> Bool
+nor = not . or
+
-- | Applied to a predicate and a list, 'any' determines if any element
-- of the list satisfies the predicate. For the result to be
-- 'False', the list must be finite; 'True', however, results from a 'True'
@@ -556,6 +572,20 @@ all p (x:xs) = p x && all p xs
#-}
#endif
+-- | Applied to a predicate and a list, 'nany' determines if no element
+-- of the list satisfies the predicate. For the result to be
+-- 'True', the list must be finite; 'False', however, results from a 'True'
+-- value for the predicate applied to an element at a finite index of a finite or infinite list.
+nany :: (a -> Bool) -> [a] -> Bool
+nany p = not . any p
+
+-- | Applied to a predicate and a list, 'nall' determines if not all elements
+-- of the list satisfy the predicate. For the result to be
+-- 'False', the list must be finite; 'True', however, results from a 'False'
+-- value for the predicate applied to an element at a finite index of a finite or infinite list.
+nall :: (a -> Bool) -> [a] -> Bool
+nall p = not . all p
+
-- | 'elem' is the list membership predicate, usually written in infix form,
-- e.g., @x \`elem\` xs at . For the result to be
-- 'False', the list must be finite; 'True', however, results from an element equal to @x@ found at a finite index of a finite or infinite list.
diff --git a/changelog.md b/changelog.md
index 88ceec5..20c90f7 100644
--- a/changelog.md
+++ b/changelog.md
@@ -125,4 +125,7 @@
* Remove deprecated function `unsafePerformIO` export from `Foreign`
(still available via `System.IO.Unsafe.unsafePerformIO`).
+ * Implement negation-shortands for `and`, `or`, `any` and `all` in
+ `Data.List`: `nand`, `nor`, `nany`, and `nall`.
+
* Various fixes and other improvements (see Git history for full details).
--
1.8.3.2
More information about the Libraries
mailing list