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 praktikum

Selles praktikumis harjutame funktsioonide kirjutamist, mis tegelikult on Haskelli standardteegis olemas. Näeme, kuidas Haskelli listidega fundamentaalsel tasandil opereerida; näeme, et need standardfunktsioonid pole mingi sisseehitatud maagia, ning selle käigus õpime ära mõned lihtsamad standardteegi funktsioonid.

Selleks, et meie definitsioonid ei oleks standardteegiga (Prelude) konfliktis, lisa faili algusesse järgnev:

-- Peidame samanimelised standardteegi definitsioonid
import Prelude hiding (fst, head, tail, length, (++), (!!), take,
  snd, null, sum, last, drop, reverse, lines)

Näiteülesanded

fst -- paari esimene element

fst :: (a, b) -> a
fst = undefined

head -- listi esimene element

head :: [a] -> a
head = undefined

tail -- listi "saba"

tail :: [a] -> [a]
tail = undefined 

length -- listi pikkuse arvutamine

length :: [a] -> Int
length = undefined

Redutseeri avaldis length [2, 3, 4] .

++ -- kahe listi konkateneerimine

infixr 5 ++
(++) :: [a] -> [a] -> [a]
(++) = undefined

Redutseeri avaldis [1] ++ [2].

!! -- listi indekseerimine

infixl 9 !!
(!!) :: [a] -> Int -> a
(!!) = undefined

Redutseeri avaldis [1, 2] !! 1.

take -- listist algusosa võtmine

take :: Int -> [a] -> [a]
take = undefined

Harjutusülesanded

Ülesanne 1 -- snd -- paari teine element

snd :: (a, b) -> b
snd = undefined

Näiteks:

snd (1, "bla")    ==> "bla"
snd (2, "bla")    ==> "bla"
snd (False, True) ==> True

Ülesanne 2 -- null -- tühilisti kontroll

null :: [a] -> Bool
null = undefined

Näiteks:

null [1, 2, 3] ==> False
null [[]]      ==> False
null []        ==> True

Ülesanne 3 -- sum -- listi elementide summa

Kirjutada funktsioon sum, mis leiab täisarvude listi summa.

sum :: [Integer] -> Integer
sum = undefined

Näiteks:

sum [1..10]   ==>   55
sum [3, 1, 2] ==>   6
sum [2]       ==>   2
sum []        ==>   0

Redutseeri avaldis sum [100, 76, 24].

Ülesanne 4 -- last -- listi viimane element

last :: [a] -> a
last = undefined

Näiteks:

last [1]       ==>   1
last [1, 2, 3] ==>   3
last [1..10]   ==>   10
last "kala"    ==>   'a'

Redutseeri avaldis last [1,2,3,4].

Ülesanne 5 -- drop -- listi algusosa eemalejätmine

drop :: Int -> [a] -> [a]
drop = undefined

Näiteks:

drop 0 [1, 2, 3, 4]    ==> [1, 2, 3, 4]
drop 1 [1, 2, 3, 4]    ==> [2, 3, 4]
drop 3 [1, 2, 3, 4]    ==> [4]
drop 100 [1, 2, 3, 4]  ==> []

Ülesanne 6 -- reverse -- listi ümberpööramine

reverse :: [a] -> [a]
reverse = undefined

Näiteks:

reverse [1, 2, 3]     ==> [3, 2, 1]
reverse [True, False] ==> [False, True]
reverse []            ==> []

Ülesanne 7 -- multPairs -- listi paaride korrutamine

Kirjuta funktsioon multPairs, mis korrutab täisarvupaaride listi iga paari elemendid omavahel. (See funktsioon pole standardne)

multPairs :: [(Integer, Integer)] -> [Integer]
multPairs = undefined

Näiteks:

multPairs [(2, 3)]                  ==> [6]
multPairs [(2, 3), (7, 5), (0, 10)] ==> [6, 35, 0]

Ülesanded*

Kirjuta funktsioon lines, mis võtab sisse sõne ja tagastab sõnede listi, mille elemendid on sisendi (reavahetusega eraldatud) teksitread.

lines :: String -> [String]
lines = undefined

Näiteks:

lines ""           ==> []
lines "asfd"       ==> ["asfd"]
lines "asfd\n"     ==> ["asfd"]
lines "as\nfd\n"   ==> ["as", "fd"]
lines "as\nfd\n\n" ==> ["as", "fd", ""]
  • 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