Export translations
Jump to navigation
Jump to search
Settings
Group
FP Homework 1
FP Laboratory 1
FP Laboratory 10
FP Laboratory 11
FP Laboratory 12
FP Laboratory 2
FP Laboratory 3
FP Laboratory 4
FP Laboratory 5
FP Laboratory 6
FP Laboratory 7
FP Laboratory 9
Functional programming
Main Page
Language
aa - Afar
ab - Abkhazian
abs - Ambonese Malay
ace - Achinese
ady - Adyghe
ady-cyrl - Adyghe (Cyrillic script)
aeb - Tunisian Arabic
aeb-arab - Tunisian Arabic (Arabic script)
aeb-latn - Tunisian Arabic (Latin script)
af - Afrikaans
ak - Akan
aln - Gheg Albanian
am - Amharic
an - Aragonese
ang - Old English
anp - Angika
ar - Arabic
arc - Aramaic
arn - Mapuche
arq - Algerian Arabic
ary - Moroccan Arabic
arz - Egyptian Arabic
as - Assamese
ase - American Sign Language
ast - Asturian
atj - Atikamekw
av - Avaric
avk - Kotava
awa - Awadhi
ay - Aymara
az - Azerbaijani
azb - South Azerbaijani
ba - Bashkir
ban - Balinese
bar - Bavarian
bbc - Batak Toba
bbc-latn - Batak Toba (Latin script)
bcc - Southern Balochi
bcl - Central Bikol
be - Belarusian
be-tarask - Belarusian (Taraškievica orthography)
bg - Bulgarian
bgn - Western Balochi
bh - Bhojpuri
bho - Bhojpuri
bi - Bislama
bjn - Banjar
bm - Bambara
bn - Bangla
bo - Tibetan
bpy - Bishnupriya
bqi - Bakhtiari
br - Breton
brh - Brahui
bs - Bosnian
btm - Batak Mandailing
bto - Iriga Bicolano
bug - Buginese
bxr - Russia Buriat
ca - Catalan
cbk-zam - Chavacano
cdo - Min Dong Chinese
ce - Chechen
ceb - Cebuano
ch - Chamorro
cho - Choctaw
chr - Cherokee
chy - Cheyenne
ckb - Central Kurdish
co - Corsican
cps - Capiznon
cr - Cree
crh - Crimean Turkish
crh-cyrl - Crimean Tatar (Cyrillic script)
crh-latn - Crimean Tatar (Latin script)
cs - Czech
csb - Kashubian
cu - Church Slavic
cv - Chuvash
cy - Welsh
da - Danish
de - German
de-at - Austrian German
de-ch - Swiss High German
de-formal - German (formal address)
din - Dinka
diq - Zazaki
dsb - Lower Sorbian
dtp - Central Dusun
dty - Doteli
dv - Divehi
dz - Dzongkha
ee - Ewe
egl - Emilian
el - Greek
eml - Emiliano-Romagnolo
en - English
en-ca - Canadian English
en-gb - British English
eo - Esperanto
es - Spanish
es-419 - Latin American Spanish
es-formal - español (formal)
et - Estonian
eu - Basque
ext - Extremaduran
fa - Persian
ff - Fulah
fi - Finnish
fit - Tornedalen Finnish
fj - Fijian
fo - Faroese
fr - French
frc - Cajun French
frp - Arpitan
frr - Northern Frisian
fur - Friulian
fy - Western Frisian
ga - Irish
gag - Gagauz
gan - Gan Chinese
gan-hans - Gan (Simplified)
gan-hant - Gan (Traditional)
gcr - Guianan Creole
gd - Scottish Gaelic
gl - Galician
glk - Gilaki
gn - Guarani
gom - Goan Konkani
gom-deva - Goan Konkani (Devanagari script)
gom-latn - Goan Konkani (Latin script)
gor - Gorontalo
got - Gothic
grc - Ancient Greek
gsw - Swiss German
gu - Gujarati
gv - Manx
ha - Hausa
hak - Hakka Chinese
haw - Hawaiian
he - Hebrew
hi - Hindi
hif - Fiji Hindi
hif-latn - Fiji Hindi (Latin script)
hil - Hiligaynon
ho - Hiri Motu
hr - Croatian
hrx - Hunsrik
hsb - Upper Sorbian
ht - Haitian Creole
hu - Hungarian
hu-formal - magyar (formal)
hy - Armenian
hyw - Western Armenian
hz - Herero
ia - Interlingua
id - Indonesian
ie - Interlingue
ig - Igbo
ii - Sichuan Yi
ik - Inupiaq
ike-cans - Eastern Canadian (Aboriginal syllabics)
ike-latn - Eastern Canadian (Latin script)
ilo - Iloko
inh - Ingush
io - Ido
is - Icelandic
it - Italian
iu - Inuktitut
ja - Japanese
jam - Jamaican Creole English
jbo - Lojban
jut - Jutish
jv - Javanese
ka - Georgian
kaa - Kara-Kalpak
kab - Kabyle
kbd - Kabardian
kbd-cyrl - Kabardian (Cyrillic script)
kbp - Kabiye
kg - Kongo
khw - Khowar
ki - Kikuyu
kiu - Kirmanjki
kj - Kuanyama
kjp - Eastern Pwo
kk - Kazakh
kk-arab - Kazakh (Arabic script)
kk-cn - Kazakh (China)
kk-cyrl - Kazakh (Cyrillic script)
kk-kz - Kazakh (Kazakhstan)
kk-latn - Kazakh (Latin script)
kk-tr - Kazakh (Turkey)
kl - Kalaallisut
km - Khmer
kn - Kannada
ko - Korean
ko-kp - Korean (North Korea)
koi - Komi-Permyak
kr - Kanuri
krc - Karachay-Balkar
kri - Krio
krj - Kinaray-a
krl - Karelian
ks - Kashmiri
ks-arab - Kashmiri (Arabic script)
ks-deva - Kashmiri (Devanagari script)
ksh - Colognian
ku - Kurdish
ku-arab - Kurdish (Arabic script)
ku-latn - Kurdish (Latin script)
kum - Kumyk
kv - Komi
kw - Cornish
ky - Kyrgyz
la - Latin
lad - Ladino
lb - Luxembourgish
lbe - Lak
lez - Lezghian
lfn - Lingua Franca Nova
lg - Ganda
li - Limburgish
lij - Ligurian
liv - Livonian
lki - Laki
lmo - Lombard
ln - Lingala
lo - Lao
loz - Lozi
lrc - Northern Luri
lt - Lithuanian
ltg - Latgalian
lus - Mizo
luz - Southern Luri
lv - Latvian
lzh - Literary Chinese
lzz - Laz
mai - Maithili
map-bms - Basa Banyumasan
mdf - Moksha
mg - Malagasy
mh - Marshallese
mhr - Eastern Mari
mi - Maori
min - Minangkabau
mk - Macedonian
ml - Malayalam
mn - Mongolian
mni - Manipuri
mnw - Mon
mo - Moldovan
mr - Marathi
mrj - Western Mari
ms - Malay
mt - Maltese
mus - Creek
mwl - Mirandese
my - Burmese
myv - Erzya
mzn - Mazanderani
na - Nauru
nah - Nāhuatl
nan - Min Nan Chinese
nap - Neapolitan
nb - Norwegian Bokmål
nds - Low German
nds-nl - Low Saxon
ne - Nepali
new - Newari
ng - Ndonga
niu - Niuean
nl - Dutch
nl-informal - Nederlands (informeel)
nn - Norwegian Nynorsk
no - Norwegian
nov - Novial
nrm - Norman
nso - Northern Sotho
nv - Navajo
ny - Nyanja
nys - Nyungar
oc - Occitan
olo - Livvi-Karelian
om - Oromo
or - Odia
os - Ossetic
pa - Punjabi
pag - Pangasinan
pam - Pampanga
pap - Papiamento
pcd - Picard
pdc - Pennsylvania German
pdt - Plautdietsch
pfl - Palatine German
pi - Pali
pih - Norfuk / Pitkern
pl - Polish
pms - Piedmontese
pnb - Western Punjabi
pnt - Pontic
prg - Prussian
ps - Pashto
pt - Portuguese
pt-br - Brazilian Portuguese
qqq - Message documentation
qu - Quechua
qug - Chimborazo Highland Quichua
rgn - Romagnol
rif - Riffian
rm - Romansh
rmy - Vlax Romani
rn - Rundi
ro - Romanian
roa-tara - Tarantino
ru - Russian
rue - Rusyn
rup - Aromanian
ruq - Megleno-Romanian
ruq-cyrl - Megleno-Romanian (Cyrillic script)
ruq-latn - Megleno-Romanian (Latin script)
rw - Kinyarwanda
sa - Sanskrit
sah - Sakha
sat - Santali
sc - Sardinian
scn - Sicilian
sco - Scots
sd - Sindhi
sdc - Sassarese Sardinian
sdh - Southern Kurdish
se - Northern Sami
sei - Seri
ses - Koyraboro Senni
sg - Sango
sgs - Samogitian
sh - Serbo-Croatian
shi - Tachelhit
shi-latn - Tachelhit (Latin script)
shi-tfng - Tachelhit (Tifinagh script)
shn - Shan
shy-latn - Shawiya (Latin script)
si - Sinhala
simple - Simple English
sk - Slovak
skr - Saraiki
skr-arab - Saraiki (Arabic script)
sl - Slovenian
sli - Lower Silesian
sm - Samoan
sma - Southern Sami
sn - Shona
so - Somali
sq - Albanian
sr - Serbian
sr-ec - Serbian (Cyrillic script)
sr-el - Serbian (Latin script)
srn - Sranan Tongo
ss - Swati
st - Southern Sotho
stq - Saterland Frisian
sty - cебертатар
su - Sundanese
sv - Swedish
sw - Swahili
szl - Silesian
ta - Tamil
tay - Tayal
tcy - Tulu
te - Telugu
tet - Tetum
tg - Tajik
tg-cyrl - Tajik (Cyrillic script)
tg-latn - Tajik (Latin script)
th - Thai
ti - Tigrinya
tk - Turkmen
tl - Tagalog
tly - Talysh
tn - Tswana
to - Tongan
tpi - Tok Pisin
tr - Turkish
tru - Turoyo
ts - Tsonga
tt - Tatar
tt-cyrl - Tatar (Cyrillic script)
tt-latn - Tatar (Latin script)
tum - Tumbuka
tw - Twi
ty - Tahitian
tyv - Tuvinian
tzm - Central Atlas Tamazight
udm - Udmurt
ug - Uyghur
ug-arab - Uyghur (Arabic script)
ug-latn - Uyghur (Latin script)
uk - Ukrainian
ur - Urdu
uz - Uzbek
uz-cyrl - Uzbek (Cyrillic script)
uz-latn - Uzbek (Latin script)
ve - Venda
vec - Venetian
vep - Veps
vi - Vietnamese
vls - West Flemish
vmf - Main-Franconian
vo - Volapük
vot - Votic
vro - Võro
wa - Walloon
war - Waray
wo - Wolof
wuu - Wu Chinese
xal - Kalmyk
xh - Xhosa
xmf - Mingrelian
xsy - Saisiyat
yi - Yiddish
yo - Yoruba
yue - Cantonese
za - Zhuang
zea - Zeelandic
zgh - Standard Moroccan Tamazight
zh - Chinese
zh-cn - Chinese (China)
zh-hans - Simplified Chinese
zh-hant - Traditional Chinese
zh-hk - Chinese (Hong Kong)
zh-mo - Chinese (Macau)
zh-my - Chinese (Malaysia)
zh-sg - Chinese (Singapore)
zh-tw - Chinese (Taiwan)
zu - Zulu
Format
Export for off-line translation
Export in native format
Fetch
{{DISPLAYTITLE:FP Laboratory 3}}== Usage of lists == Find out the results of the following operations: <syntaxhighlight lang="Haskell"> [3,2,1] > [2,1,0] [3,2,1] > [2,10,100] [3,4,2] > [3,4] [3,4,2] > [2,4] [3,4,2] == [3,4,2] </syntaxhighlight> == Simple functions working with list == Implement following functions: * Create a function that computes length of a list. <syntaxhighlight lang="Haskell">length' :: [a] -> Int</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> length' "ABCD" 4 </syntaxhighlight> <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> <syntaxhighlight lang="Haskell"> length' :: [a] -> Int length' [] = 0 length' (_:xs) = 1 + length' xs </syntaxhighlight> [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] </div> <div style="clear:both"></div> * Create a function that sums the list of integers. <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/Yuz8lpKZiJs]]</div> <syntaxhighlight lang="Haskell">sumIt :: [Int] -> Int</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> sumIt [1,2,3] 6 </syntaxhighlight> <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> <syntaxhighlight lang="Haskell"> sumIt :: [Int] -> Int sumIt [] = 0 sumIt (x:xs) = x + sumIt xs </syntaxhighlight> [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] </div> <div style="clear:both"></div> * Create a function that returns the first element in the list. <syntaxhighlight lang="Haskell">getHead :: [a] -> a</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> getHead [1,2,3] 1 </syntaxhighlight> <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> <syntaxhighlight lang="Haskell"> getHead :: [a] -> a getHead (x:_) = x </syntaxhighlight> [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] </div> <div style="clear:both"></div> * Create a function that returns the last element in the list. <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/I5AZu8_G8pQ]]</div> <syntaxhighlight lang="Haskell">getLast :: [a] -> a</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> getLast [1,2,3] 3 </syntaxhighlight> <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> <syntaxhighlight lang="Haskell"> getLast :: [a] -> a getLast [x] = x getLast (x:xs) = getLast xs getLast' :: [a] -> a getLast' (x:xs) | length xs == 0 = x | otherwise = getLast' xs </syntaxhighlight> [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] </div> <div style="clear:both"></div> * Create a function that checks if an element is a member of the list. <syntaxhighlight lang="Haskell">isElement :: Eq a => a -> [a] -> Bool</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> isElement 2 [1,2,3] True </syntaxhighlight> <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> <syntaxhighlight lang="Haskell"> isElement :: Eq a => a -> [a] -> Bool isElement _ [] = False isElement a (x:xs) | a == x = True | otherwise = isElement a xs </syntaxhighlight> [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] </div> <div style="clear:both"></div> * Create a function that returns the list without the first element. <syntaxhighlight lang="Haskell">getTail :: [a] -> [a]</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> getTail [1,2,3] [2,3] </syntaxhighlight> <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> <syntaxhighlight lang="Haskell"> getTail :: [a] -> [a] getTail (_:xs) = xs </syntaxhighlight> [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] </div> <div style="clear:both"></div> * Create a function that returns the list without the last element. <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/ZLmvYTZ6fkw]]</div> <syntaxhighlight lang="Haskell">getInit :: [a] -> [a]</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> getInit [1,2,3] [1,2] </syntaxhighlight> <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> <syntaxhighlight lang="Haskell"> getInit :: [a] -> [a] getInit [_] = [] getInit (x:xs) = x : getInit xs </syntaxhighlight> [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] </div> <div style="clear:both"></div> * Create a function that merge two lists into one list. <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/FFoHC-g3f_g]]</div> <syntaxhighlight lang="Haskell">combine :: [a] -> [a] -> [a]</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> combine [1,2,3] [4,5] [1,2,3,4,5] </syntaxhighlight> <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> <syntaxhighlight lang="Haskell"> combine :: [a] -> [a] -> [a] combine [] y = y combine (x:xs) y = x : combine xs y </syntaxhighlight> [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] </div> <div style="clear:both"></div> * Create a function that finds the maximum in the list of integers. <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/ybgM-YeQTco]]</div> <syntaxhighlight lang="Haskell">max' :: [Int] -> Int</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> max' [3,1,7,5] 7 </syntaxhighlight> <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> <syntaxhighlight lang="Haskell"> max' :: [Int] -> Int max' [x] = x max' (x:y:z) | x > y = max' (x:z) | otherwise = max' (y:z) max'' :: [Int] -> Int max'' (y:ys) = tmp y ys where tmp a [] = a tmp a (x:xs) | x > a = tmp x xs |otherwise = tmp a xs </syntaxhighlight> [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] </div> <div style="clear:both"></div> * Create a function that reverse a list. <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/jlUuZ5fCFgQ]]</div> <syntaxhighlight lang="Haskell">reverse' :: [a] -> [a]</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> reverse' [3,1,7,5] [5,7,1,3] </syntaxhighlight> <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> <syntaxhighlight lang="Haskell"> reverse' :: [a] -> [a] reverse' [] = [] reverse' (x:xs) = (reverse' xs) ++ [x] reverse'' :: [a] -> [a] reverse'' n = tmp n [] where tmp [] ys = ys tmp (x:xs) ys = tmp xs (x:ys) </syntaxhighlight> [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] </div> <div style="clear:both"></div> * Create a function that product scalar multiplication of two vectors. <syntaxhighlight lang="Haskell">scalar :: [Int] -> [Int] -> Int</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> scalar [1,2,3] [4,5,6] 32 </syntaxhighlight> <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> <syntaxhighlight lang="Haskell"> scalar :: [Int] -> [Int] -> Int scalar [] [] = 0 scalar (x:xs) (y:ys) = x*y + scalar xs ys </syntaxhighlight> [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] </div> <div style="clear:both"></div> = Additional exercises = * Create a function that eliminates all occurrences of zeros in a list. <syntaxhighlight lang="Haskell">nonZeros :: [Int] -> [Int]</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> nonZeros [0,1,0,2,3,0,0] [1,2,3] </syntaxhighlight> * Create a function that realizes the left rotation of a list by one element. <syntaxhighlight lang="Haskell">rotateLeft1 :: [a] -> [a]</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> rotateLeft1 [1,2,3,4,5] [2,3,4,5,1] </syntaxhighlight> * Create a function that realizes the right rotation of a list by one element. <syntaxhighlight lang="Haskell">rotateRight1 :: [a] -> [a]</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> rotateRight1 [1,2,3,4,5] [5,1,2,3,4] </syntaxhighlight> * Create a function that eliminates all even numbers from a list. <syntaxhighlight lang="Haskell">oddMembers :: [Int] -> [Int]</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> oddMembers [0,1,0,2,3,0,0] [1,3] </syntaxhighlight> * Create a function that counts all odd numbers in a given list (define non-recursively using previously defined functions). <syntaxhighlight lang="Haskell">countOddMembers :: [Int] -> Int</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> countOddMembers [1,0,3,1,4,5] 4 </syntaxhighlight> * Create a function that compares lists of numbers if they are equal. <syntaxhighlight lang="Haskell">compareLists :: Eq a => [a] -> [a] -> Bool </syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> compareLists [5,8,11] [5,8,11] True *Main> compareLists [5,8,11] [3,5,8] False *Main> compareLists [] [] True *Main> compareLists [5,8,11] [5,8,11,13] False </syntaxhighlight>
Navigation menu
Personal tools
English
Log in
Namespaces
Special page
Variants
Views
More
Search
Main page
Education
Functional programming
Functional programming - presentations
Functional programming - laboratories
Programming Languages and Compilers
Practical Functional Programming
History Entries
Programming paradigms
C#
Tools
Special pages