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

Programming Languages 2018/19 fall

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

Kaheksas praktikum

Eksami Haskelli ülesannete näidised.

1.

Kirjuta funktsioon f, mis saab argumendiks kolmikute (x,y,z) listi ja tagastab paari (a,b). Tagastatud paari teine komponent b on argumentide kolmandate komponentide z summa. Tagastatud paari esimene komponent a on list mis koosneb argumendi esimestest komponentidest x, kusjuures x esineb a-s y korda ning x-d on a-s vastupidises järjekorras.

Näiteks:

-- []                              ->  ("",0)
-- [('a', 1, 1.0)]                 ->  ("a", 1.0)
-- [('x', 2, 4.0), ('y', 3, 1.0)]  ->  ("yyyxx",5)
-- [('x', 2, 4.0), ('y', 3, 2.0)]  ->  ("yyyxx",6)
-- [('x', 2, 4.0), ('y', 1, 1.0)]  ->  ("yxx",5)
-- [('x', 2, 4.0), ('y', 0, 1.0)]  ->  ("xx",5)
-- [('x', 2, 4.0), ('y', 3, 2.0), ('z',2,1.0)]  ->  ("zzyyyxx",7)

f :: [(a, Int, Double)] -> ([a], Double)
f = undefined

2.

Kasutades foldr, kirjuta funktsioon g, mis võtab argumendiks täisarvude listi ja tagastab ()-de listi. Tühjale listile vastata väärtusega [(),(),(),()] ning iga 0-i kohta argumendis tuleb tagastada ühe võrra vähem ()-ge.

Näiteks:

-- []            --> [(),(),(),()]
-- [0,0,1]       --> [(),()]
-- [0,1,0]       --> [(),()]
-- [1,0,0]       --> [(),()]
-- [8,0,9]       --> [(),(),()]
-- [0,0,0]       --> [()]
-- [0,0,0,0]     --> []
-- [0,0,0,0,0,0] --> []

g :: [Int] -> [()]
g = foldr undefined undefined

3.

Kirjuta protseduur h, mis võtab kaks täisarvulist argumenti: n ja m. Protseduur genereerib (randomRIO-ga näiteks) n juhuslikku täisarvu lõigust [0, m]. Esmalt trükitakse konsooli genereeritud arvude summa ning seejärel genereeritud arvud (iga arv eri reale).

Näiteks:

-- > h 3 10
-- 14
-- 7
-- 5
-- 2
h :: Int -> Int -> IO ()
h = undefined

4.

Olgu antud järgnev andmestruktuur, kus E on graafi servade tüübipere ja G on graafide tüübipere.

data E a = E a String a  deriving (Eq)
data G a = G [a] [E a]   deriving (Eq, Show)

instance Show a => Show (E a) where
  show (E _ s _) = s

-- Näiteks on antud selline graaf:
g1 = G [1,2,3,4] [E 1 "a" 2, E 2 "b" 3, E 2 "c" 4, E 4 "d" 1]

Kirjutage funktsioon teedAlates g x ys , kus g on graaf, x on tipp ja ys on tippude list. Funktsioon peab väljastama kõikide mittetsükliliste teede listi, mis lähtuvad tipust x ja mis ei sisalda tippe listist ys.

Näiteks:

> teedAlates  g1 1 []
[[],[a],[a,b],[a,c]]

> teedAlates  g1 2 []
[[],[b],[c],[c,d]]
teedAlates :: Eq a => G a -> a -> [a] -> [[E a]]
teedAlates = undefined

5+

Kodutöö küsimused (või nende puudumisel eelmiste nädalate poolelijäänud ülesanded).

  • 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