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 Praktikum

Näiteülesanded

fst -- paari esimene element

-- järgnevad funktsioonid on standardteegis juba defineeritud
import Prelude hiding (head, tail, null, length, (++), concat,
    (!!), take, drop, reverse, lines, fst, snd)

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].

!! -- listist n-nda elemendi võtmine

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äited:
-- snd (1,"bla")     ==> "bla"
-- snd (2,"bla")     ==> "bla"
-- snd (False, True) ==> True

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

null :: [a] -> Bool
null = undefined
-- Näited:
-- null [1,2,3]       ==> False
-- null [[]]          ==> False
-- null []            ==> True

Ülesanne 3 -- drop -- listi lõpuosa võtmine

drop :: Int -> [a] -> [a]
drop = undefined
-- Näited:
-- 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 4 -- reverse -- listi ümberpööramine

reverse :: [a] -> [a]
reverse = undefined
-- Näited:
-- reverse [1,2,3]       ==> [3,2,1]
-- reverse [True, False] ==> [False, True]
-- reverse []            ==> []

Ülesanne 5 -- sumList -- listi elementide summa

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

sumList :: [Integer] -> Integer
sumList = undefined
-- Näited:
-- sumList [1..10]   ==>   55
-- sumList [3,1,2]   ==>   6
-- sumList [2]       ==>   2
-- sumList []        ==>   0

Redutseeri avaldis sumList [100,76,24].

Ülesanne 6 -- lastElem -- listi viimane element

lastElem :: [a] -> a
lastElem = undefined
-- Näited:
--   lastElem [1]      ==>   1
--   lastElem [1,2,3]  ==>   3
--   lastElem [1..10]  ==>   10
--   lastElem "kala"   ==>   'a'

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

Ülesanded*

Ülesanne 7*

Kirjuta funktsioon lines, mis võtab sisse sõne ja tagastab sõnede listi, mille elemendid on sisendi (reavahetusega eraldatud) teksitread. Ülesannet võib lahendada abifunktsiooniga, kus on lisaks argumendid juba loetud ridade listi ja lugemisel olev poolik rea jaoks.

lines :: String -> [String]
lines = undefined
-- Näited:
-- 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