module HW4 where

import Hugs.Prelude

 

 

{-######################################

 QUESTION 5.18

########################################-}

 

shift :: ((Int,Int),Int)->(Int,(Int,Int))

shift ((x,y),z)=(x,(y,z))

 

 

shift2 ((x,y),z)=(x,(y,z))

 

-- ANSWER :

-- if we omit the type declaration, all the types

-- can be used, even when the types are differents

 

{-######################################

 QUESTION 9.2

  

  length [1,2,3,4] ===> 4

########################################-}

 

length ::[a]->Int

length x=sum (map (ret1) x) where ret1 x=1

 

{-######################################

 QUESTION 9.3

 

addUp ns=map fun1 (filter fun2 ns)  ???

 

addUp  [0,1,2,3] ===> [2,3,4]

addUp2 [0,1,2,3] ===> [2,3,4]

########################################-}

 

-- ***** BEFORE *****

greaterOne :: Int->Bool

greaterOne n=n>1

 

addOne :: Int->Int

addOne n=n+1

 

addUp ns=filter greaterOne (map addOne ns)

 

-- ****** AFTER ******

greaterZero :: Int->Bool

greaterZero n= n>0

 

addUp2 ns=map addOne (filter greaterZero ns)

 

{-######################################

 QUESTION 9.6

########################################-}

 

-- 1

squareList::[Int]->[Int]

squareList ns=map (^2) ns

 

-- 2

sumSquare ::[Int]->Int

sumSquare ns=sum ( squareList ns )

 

-- 3

biggerThanZero::[Int]->Bool

biggerThanZero ns=and(map (>0) ns)

 

{-######################################

 QUESTION 9.7

########################################-}

 

-- Test functions

 

funcAlt :: Int->Float

funcAlt x= (sqrt (fromInt x))*sin((fromInt x)*150.0)

 

funcInc :: Int->Int

funcInc x=2*x

 

funcDec :: Int->Int

funcDec x= negate(2*x)

 

funcSame :: Int->Float

funcSame x=5

 

funcZero :: Int->Float

funcZero x=0

 

 

-- ******** ANSWER ********

 

-- give the minimum for f(0..n)

getMinFunc func n= getMin( map func [0..n] )

   where getMin (x:xs)

          | xs==[] =x

          | otherwise=getMin( [el | el<-xs, el < x] ++ [x])

 

-- test if values are all equal for f(0..n)

equalFunc func n=allEqual( map func [0..n] )

    where allEqual []=True

          allEqual (x:xs)   =and (map (==x) xs)

       

 

-- test if values are all bigger than 0 for f(0..n)

biggerZeroFunc func n=isBigger ( map func [0..n])

    where isBigger []=True

          isBigger ls =and ( map (>0) ls )

 

 

-- test if values are increasing for f(0..n)

increaseFunc func n=isInc(map func [0..n])

  where isInc []=False

              isInc (x:xs)

                | null(xs) = True

                | x<=head xs    = isInc xs

               | otherwise     = False

 

{-######################################

 QUESTION 9.8

   twice f x  ====> f(f x)

########################################-}

 

twice::(Int->Int)->Int->Int

twice f n = f (f n)

 

-- OR.....

 

twice2::(a->a)->a->a

twice2 f n = f (f n)

 

{-######################################

 QUESTION 9.9

 

   iter n f x ===> f( f ( f...(f x) ) )

   iter 3 f x ===> f( f( f x ) )

########################################-}

 

iter :: Int -> (a -> a) -> a -> a

iter 0 f x =x

iter n f x=iter (n-1) f (f x)

 

{-######################################

 QUESTION 9.10

 

    pow2n ===> 2^n

########################################-}

 

double::Int->Int

double n=n*2

 

pow2n::Int->Int

pow2n n=iter n double 1

 

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

-- #######  E N D ########################

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