From posting-system@google.com Fri Jul 13 18:11:20 2001 Date: Fri, 13 Jul 2001 17:45:22 -0700 Reply-To: posting-system@google.com From: oleg@pobox.com (oleg@pobox.com) Newsgroups: comp.lang.functional Subject: Re: bothTrue References: <9imd0o$nka$1@tomahawk.unsw.edu.au> <9immfv$qi2$1@tomahawk.unsw.edu.au> Message-ID: <7eb8ac3e.0107131645.4949988c@posting.google.com> Status: OR The question was to implement a function bothtruth (which returns True iff both arguments are True) in such a way so to avoid function (&&) and the pattern-matching facilities of Haskell. Note that functions 'not' and (||) are defined in the Prelude with the use of pattern-matching. The 'if' function may rely on pattern-matching as well. One solution: bothtruth1:: Bool -> Bool -> Bool bothtruth1 a b = (fromEnum a) + (fromEnum b) == 2 fromEnum does not _explicitly_ use the pattern matching (but it may implicitly do, in the compiler-derived code). Another solution: bothtruth':: Bool -> Bool -> Bool bothtruth' a b = (a,b) == (True,True) according to the Hugs prelude, a tuple identity comparison function is built into the compiler. The following is a "better" solution. It seems to get by without even implicit pattern matching. It requires Hugs: primitive ptr_to_int "unsafePtrToInt" :: a -> Int bothtruth2:: Bool -> Bool -> Bool bothtruth2 a b = a `seq` b `seq` (ptr_to_int a - ptr_to_int False) + (ptr_to_int b - ptr_to_int False) == 2*(ptr_to_int True - ptr_to_int False) To verify: bothtruth2 True True -- --> True bothtruth2 (1==1) True -- --> True bothtruth2 (1==2) (1==2) -- --> False bothtruth2 (1==1) (2==2) -- --> True bothtruth2 (odd 2) (2==2) -- --> False bothtruth2 (odd 1) ("Haskell" == "Haskell") -- --> True