module HW5 where

import Hugs.Prelude

 

-- Week 5, midterm interview

 

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

 QUESTION 9.11

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

 

sumSqr::Int -> Int

sumSqr n=foldr1 (+) (map sqr [0..n]) where sqr x=x*x

 

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

 QUESTION 9.12

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

 

sumSqrList::[Int] -> Int

sumSqrList ls=foldr1 (+) (map sqr ls) where sqr x=x*x

 

 

sumSqrListInt::[Int]->Int

sumSqrListInt ls=sumSqrList (filter positive ls)

     where positive x=x>0

 

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

 QUESTION 9.13

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

 

unzip :: [(a, b)] -> ([a], [b])

unzip [  ]=([],[])

unzip xs=( foldr (:) [] (map fst xs),foldr (:) [] (map snd xs) )

 

last::[a]->a

last n=foldr1 getSec n

      where getSec a b=b

 

init::[a]->[a]

init ls=take ((length ls)-1) (foldr (:) [] ls)

 

-- "Classic" way

 

init2::[a]->[a]

init2 x=reverse (skipfst (reverse x))

     where skipfst  []=[]

                skipfst  (y:ys)=ys

   

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

 QUESTION 9.14

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

 

mystery xs = foldr (++) [] (map sing xs)

sing x = [x]

 

{-------------------------------------------

 

 ANSWER

 the function sing "list" the argument

 this function does the following :

 

foldr (++) [] (map sing "123") ==>

1 ++ foldr (++) [] (map sing "23") ==>

1 ++ 2 ++ foldr (++) [] (map sing "3") ==>

1 ++ 2++ 3 ++ [] ==> [1,2,3]

 

 

-------------------------------------------}

 

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

 QUESTION 9.16

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

 

filterFirst::(a->Bool)->[a]->[a]

filterFirst p []=[]

filterFirst p (x:xs)

    | p x      =xs

    | otherwise=x:filterFirst p xs

 

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

 QUESTION 9.17

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

 

filterLast::(a->Bool)->[a]->[a]

filterLast p xs = reverse (filterFirst p (reverse xs))

 

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

 QUESTION 9.18

 9.7 with foldr 's

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

 

-- Test Functions

 

funcInc :: Int->Int

funcInc x=2*x

 

funcDec :: Int->Int

funcDec x= negate(2*x)

 

funcSame :: Int->Int

funcSame x=5

 

funcZero :: Int->Int

funcZero x=0

 

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

getMinFunc::(Int->Int)->Int->Int

getMinFunc func n=foldr1 min (map func [0..n])

 

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

equalFunc::(Int->Int)->Int->Bool

equalFunc func n

  | length(foldr equal [] (map func [0..n]))==length([0..n])=True

  | otherwise=False

 

equal::Int->[Int]->[Int]

equal x []=[x]

equal x (y:ys)

  | x==y=x: (equal x ys)

  | otherwise=[x]

 

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

positiveFunc::(Int->Int)->Int->Bool

positiveFunc func n

  | length(foldr posit [] (map func [0..n]))==length([0..n])=True

  | otherwise=False

 

posit::Int->[Int]->[Int]

posit x []=[x]

posit x (y:ys)

  | x>0 =x: (posit y ys)

  | x<=0 =posit y ys

  | otherwise=[x]

 

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

increaseFunc::(Int->Int)->Int->Bool

increaseFunc func n

  | length(foldr increase [] (map func [0..n]))==length([0..n])=True

  | otherwise=False

 

 

increase ::Int->[Int]->[Int]

increase  x []=[x]

increase  x (y:ys)

  | x<y =x: (increase  y ys)

  | otherwise =increase  y ys

 

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