Institute of Computer Science
  1. Courses
  2. 2019/20 fall
  3. Programming Languages (MTAT.03.006)
ET
Log in

Programming Languages 2019/20 fall

  • Info
  • Õppekava
  • Moodle
  • Loengud & Praksid
  • Lisamaterjalid
  • Küsi abi! (Fleep)

Kolmas kodutöö

1. Ülesanne (2/5 punkti)

Kirjuta listi ümberpööramise funktsioon reverse', kasutades foldl-i.

Näiteks:

reverse [] ==> []
reverse [1,2,3] ==> [3,2,1]
reverse' :: [a] -> [a]
reverse' = foldl (#) a
  where
    a = undefined
    x # y = undefined

2. Ülesanne (2/5 punkti)

Kirjuta foldr-ga funktsioon eemaldaNullid, mis eemaldab listist kõik nullid. Teised listi elemendid peavad jääma samasse järjekorda.

eemaldaNullid []           ==> []
eemaldaNullid [1,0,1,0,2]  ==> [1,1,2]
eemaldaNullid [1,1,0,0,2]  ==> [1,1,2]
eemaldaNullid [0,0,0,6,0]  ==> [6]
eemaldaNullid :: [Int] -> [Int]
eemaldaNullid = foldr (#) a
  where
    a = undefined
    x # y = undefined

3. Ülesanne (2/5 punkti)

Kasutades foldr-i, kirjuta funktsioon any, mis võtab argumendiks predikaadi ja listi. Tagasta True ainult siis, kui leidub listi elemendt, mis rahuldab predikaati. (Vihje: || on disjunktsiooni operaator)

Näiteks:

any isUpper "Tere!" == True   -- kuna isUpper 'T' == True
any isUpper "terE!" == True   -- kuna isUpper 'E' == True
any isUpper "tere!" == False
any :: (a -> Bool) -> [a] -> Bool
any f xs = undefined

4. Ülesanne (2/5 punkti)

Kirjuta funktsioon allEqual, mis kontrollib, kas täisarvude listi kõik elemendid on võrdsed.

allEqual []      ==> True
allEqual [1,2]   ==> False
allEqual [1,1,1] ==> True
allEqual [1,2,1] ==> False
allEqual :: [Int] -> Bool
allEqual xs = undefined

5. Ülesanne (2/5 punkti)

Kasutades funktsiooni foldr, kirjuta unzip' funktsioon. Unzip' jagab paaride listi kaheks eraldi listiks.

unzip' [(1,'x'), (4,'1'), (2,'p')] == ([1, 4, 2], ['x', '1', 'p'])
unzip' [] == ([], [])
unzip' :: [(x, y)] -> ([x], [y])
unzip' = foldr f b
  where
   b = undefined 
   f = undefined 
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment