Module: PairsLogic

Author: Eugene Arthur

Date Started: 2007/04/23

Last Updated: 2007/04/23

Purpose:

The following functions all perform boolean tests on pairs and apply either

AND or OR to the result of the tests. The parameters for all the functions

are:

andor -- replaced by either: (&&) or (||) note: must be inclosed in

parenthesis.

testFst -- the boolean test to apply to the first element in the pair

testSnd -- the boolean test to apply to the first element in the pair

eg for both: (<5), (=="Female"), (=>"A"), etc.

p -- is the pair to be tested,

ps -- in the case of the mapping functions where ps denotes a list

of pairs, which of course could be an empty list or a list

containing only one pair, or many thousands of pairs.

----------------------------------------------------------------------

Copyright (C) 2007 Eugene Arthur mailto:Gene<yumagene@gmail.com>

This program is free software; you can redistribute it and/or modify

it under the terms of the GNU General Public License as published by

the Free Software Foundation; Version 2 of the License.

This program is distributed in the hope that it will be useful,

but WITHOUT ANY WARRANTY; without even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

GNU General Public License for more details.

You should have received a copy of the GNU General Public License along

with this program; if not, write to the Free Software Foundation, Inc.,

51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

>module PairsLogic where

>import MapP

======= Function: pairTest ==========

This function is for use in applying separate boolean tests

on each of the elements of the pair, and either returning the

ORed or ANDed result of those tests.

>pairTest ::

> forall a b t t1 t2.

> (t -> t1 -> t2) -> (a -> t) -> (b -> t1) -> (a, b) -> t2

>pairTest andor testFst testSnd p = andor (testFst (fst p)) (testSnd (snd p))

Usage:

In the following test the ANDed result of testing of (=="horse) to

the first element in the pair, which is "pig" {False} and the test to

determine if the 3 in the second part of the pair is less than 5 {True}

results in False as the first test was False and so the AND returned

False.

*PairsLogic> pairTest (&&) (=="horse") (<5) ("pig",3)

False

The next test is exactly the same as the ANDed one above, but the

AND ie: (&&) is replaced with an OR (||) so because (3 < 5) in the

test for the second element returns True, the OR is satisfied, and

returns True as the result

*PairsLogic> pairTest (||) (=="horse") (<5) ("pig",3)

True

======= Function: mapPairTest ======================

This function is just the mapping of pairTest over a list

of pairs returning a list of the boolean results, one result

for each pair in the list.

>mapPairTest ::

> forall t t1 t2 a b.

> (t -> t1 -> t2) -> (a -> t) -> (b -> t1) -> [(a, b)] -> [t2]

>mapPairTest andor testFst testSnd ps = map (pairTest andor testFst testSnd) ps

Usage:

*PairsLogic> mapPairTest (&&) (=="horse") (<5) [(animal,age) |animal <- ["cow","horse","pig"],age <- [1..8]]

[False,False,False,False,False,False,False,False,True,True,True,True,False,

False,False,False,False,False,False,False,False,False,False,False]

======= Function: mapPairTestWpairs ===============

This function maps the parameters as in mapPairTest, the only difference

being that the result returns a pair consisting of the original pair and

the boolean result of the ORing or ANDing of the tests.

>mapPairTestWpairs ::

> forall t t1 t2 a b.

> (t -> t1 -> t2)

> -> (a -> t)

> -> (b -> t1)

> -> [(a, b)]

> -> [((a, b), t2)]

>mapPairTestWpairs andor testFst testSnd ps = mapP (pairTest andor testFst testSnd) ps

Usage:

*PairsLogic> mapPairTestWpairs (&&) (=="horse") (<5) [(animal,age) |animal <- ["cow","horse","pig"],age <- [1..8]]

[(("cow",1),False),(("cow",2),False),(("cow",3),False),(("cow",4),False),(("cow",5),False),(("cow",6),False),

(("cow",7),False),(("cow",8),False),(("horse",1),True),(("horse",2),True),(("horse",3),True),

(("horse",4),True),(("horse",5),False),(("horse",6),False),(("horse",7),False),(("horse",8),False),(("pig",1),False),(("pig",2),False),(("pig",3),False),(("pig",4),False),(("pig",5),False),(("pig",6),False),(("pig",7),False),(("pig",8),False)]

*PairsLogic> mapPairTestWpairs (||) (=="horse") (<5) [(animal,age) |animal <- ["cow","horse","pig"],age <- [1..8]]

[(("cow",1),True),(("cow",2),True),(("cow",3),True),(("cow",4),True),(("cow",5),False),(("cow",6),False),

(("cow",7),False),(("cow",8),False),(("horse",1),True),(("horse",2),True),(("horse",3),True),

(("horse",4),True),(("horse",5),True),(("horse",6),True),(("horse",7),True),(("horse",8),True),(("pig",1),True),(("pig",2),True),(("pig",3),True),(("pig",4),True),(("pig",5),False),(("pig",6),False),(("pig",7),False),(("pig",8),False)]

======== Function putMapPairTestWpairs =============

This function maps the parameters as in mapPairTest, the only difference

being that the result returns a pair consisting of the original pair and

the boolean result of the ORing or ANDing of the tests as in

function mapPairTestWpairs, but adds the output of the results one per

line. as the output is consumed as (IO),in a putStrLn,the results

cannot used by another function. The formatting makes it much easier to

see results, but if there are many result since it is used for console

output, the results will if too numerous, scroll off the screen. Still

handy for seeing results if the data set is small enough.

>putMapPairTestWpairs ::

> forall t t1 t2 a b.

> (Show ((a, b), t2)) =>

> (t -> t1 -> t2) -> (a -> t) -> (b -> t1) -> [(a, b)] -> IO ()

>putMapPairTestWpairs andor testFst testSnd ps = putMapP (pairTest andor testFst testSnd) ps

Usage:

*PairsLogic> putMapPairTestWpairs (||) (=="horse") (<3) [(animal,age) |animal <- ["cow","horse","pig"],age <- [1..4]]

(("cow",1),True)

(("cow",2),True)

(("cow",3),False)

(("cow",4),False)

(("horse",1),True)

(("horse",2),True)

(("horse",3),True)

(("horse",4),True)

(("pig",1),True)

(("pig",2),True)

(("pig",3),False)

(("pig",4),False)