module HW2 where

import Char

 

-- ###############################################################

-- Question 3.4

-- ###############################################################

 

-- ***************************************************************

-- Method 1

-- ***************************************************************

nAnd :: Bool->Bool->Bool

nAnd a b =not(a && b)

 

 

-- ***************************************************************

-- Method 2, we use the algebra of Boole=>  NOT(A and B)=NOT(A) OR NOT (B)

-- ***************************************************************

 

nAnd2 :: Bool->Bool->Bool

nAnd2 a b =not(a) || not(b)

 

 

-- ###############################################################

-- Question 3.7

-- ###############################################################

 

threeDifferent :: Int ->Int ->Int ->Bool

threeDifferent a b c

                 |  ((a==b) || (a==c)) || (b==c) =True

                 | otherwise = False

 

-- ###############################################################

-- Question 3.8

-- ###############################################################

 

-- ***************************************************************

-- Method 1

-- ***************************************************************

 

fourEqual :: Int->Int->Int->Int->Bool

fourEqual a b c d

             | (a==b && b==c && c==d)=True

             | otherwise=False 

 

-- ***************************************************************

-- Method 2, fourEqual that uses threeEqual="fourEqual2"

-- ***************************************************************

 

threeEqual :: Int->Int->Int->Bool

threeEqual a b c

             | (a==b && b==c )=True

             | otherwise=False 

 

 

fourEqual2 :: Int->Int->Int->Int->Bool

fourEqual2 a b c d

              | (threeEqual a b c) && (threeEqual b c d)=True

              | otherwise=False 

 

 

-- ###############################################################

-- Question 3.12

-- ###############################################################

-- see table at " http://www.ascii.cl/"

-- convert small character to Capital

-- if the ascii value is between {97..122} substract 32 , else leave unchanged

 

upperChar :: Char->Char

upperChar ch

          | (ord ch>=97 && ord ch<=122) =chr (ord ch - 32)

          | otherwise=ch

 

 

-- ###############################################################

-- Question 3.13

-- ###############################################################

 

-- find the ascii value of a character and substract 48

 

charToNum :: Char->Int

charToNum  ch

           |  ord ch>=48 && ord ch<58 =(ord ch - 48)

           | otherwise=0

 

-- ###############################################################

-- Question 3.17

-- ###############################################################

-- Ax^2 + BX + C =0  ?===> R1 , R2

-- take care of the case where discriminant is negative and zero, or A is zero

 

 

smallerRoot:: Float->Float->Float->Float

smallerRoot a b c

             | (b^2-4*a*c<=0) || (a==0) =0.0

             | otherwise=(-b-sqrt(b^2-4*a*c))/(2*a)

 

 

largerRoot:: Float->Float->Float->Float

largerRoot a b c

             | (b^2-4*a*c<=0) || (a==0) =0.0

             | otherwise=(-b+sqrt(b^2-4*a*c))/(2*a)

 

 

-- ###############################################################

-- Question 3.19

-- ###############################################################

-- removed the space in front of peculiar

 

funny x = x+x

peculiar y = y 

 

 

-- ANSWER: When there is a space in front of peculiar, the interpreter thinks that the second line

--         is a part of the definition of funny (even though if it is an error for the syntax)

--         When we remove the space funny become another function, funny returns the double of the argument

 

 

-- ###############################################################

--  Operator " ~> " for logical implication

-- ###############################################################

-- True  ~> True  ==>True

-- True  ~> False ==>False

-- True  ~> Null  ==>Null

-- False ~> True  ==>True

-- False ~> False ==>True

-- False ~> Null  ==>True

-- Null  ~> True  ==>True

-- Null  ~> False ==>Null

-- Null  ~> Null ==>Null

 

(~>) :: Bool->Bool->Bool

a ~> b

 | (a==True) && (b==False)=False

 | otherwise=True

 

 

mailto:avin_1066@yahoo.com