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

Programming Languages 2020/21 fall

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

2. Haskelli kodutöö

Lahenda järgmised ülesanded Haskelli moodulisse Kt2.

Moodle'ist leiad ka lahendusfaili malli Kt2.hs.

Selle kodutöö ülesanded tuleb lahendada lihtrekursiooniga, mitte standardfunktsioone kasutades.

Funktsioonide null, head, tail, fst ja snd asemel kasuta mustrisobitust.

1. ülesanne (0.4p)

Tagastab paaride listi esimesed elemendid (samas järjekorras).

Näiteks:

esimesed [] ==> []
esimesed [(2, 3), (6, 4), (9, 9)] ==> [2, 6, 9]
esimesed :: [(a, b)] -> [a]
esimesed ps = undefined

2. ülesanne (0.4p)

Tagastab True, kui arv leidub listis, muidu False.

Näiteks:

otsi 3 [1, 2, 3, 4, 5, 6] ==> True
otsi 8 [1, 2, 3, 4, 5, 6] ==> False
otsi :: Integer -> [Integer] -> Bool
otsi n xs = undefined

3. ülesanne (0.4p)

Eemaldab listi viimase elemendi.

Näiteks:

dropLast []           ==> []
dropLast [1, 2, 3]    ==> [1, 2]
dropLast [3, 2, 1, 0] ==> [3, 2, 1]
dropLast :: [a] -> [a]
dropLast xs = undefined

4. ülesanne (0.4p)

Lisab tähe sõnesse. Kusjuures nulli või negatiivse indeksiga pannakse täht algusse, suurema indeksiga, kui sõne pikkus, lõppu.

Näiteks:

lisa (-1) 'a' "xyz" ==> "axyz"
lisa 1    'a' "xyz" ==> "xayz"
lisa 3    'a' "xyz" ==> "xyza"
lisa 2    'a' "xyz" ==> "xyaz"
lisa 400  'a' "xyz" ==> "xyza"
lisa :: Int -> Char -> String -> String
lisa i x ys = undefined

5. ülesanne (0.4p)

Väärtustab polünoomi kohal x. Iga listi element (a, n) tähistab polynoomi liidetavat a*x^n.

Näiteks: [(4.0,2),(1.0,1),(30.0,0)] tähendab 4*x^2 + x + 30 ja

arvuta [(4.0, 2), (1.0, 1), (30.0, 0)] 5 ==> 135.0
arvuta [(4.0, 2), (1.0, 1), (30.0, 0)] 2 ==> 48.0
arvuta []                              5 ==> 0
arvuta :: [(Double, Int)] -> Double -> Double
arvuta ps x = undefined

Selles ülesandes kasuta astendamiseks Haskellisse sisseehitatud operaatorit ^.

  • 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