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 2}}== Types == *Using the GHCi command <code>:info</code>, learn the type of the following functions (and operators): <code>+, sqrt, succ, max</code> *Get the information about the data type of following expressions and evaluate them. it is possible using the command <code>:type</code>. You can switch this option on for all commands by <code>:set +t</code> (removing by <code>:unset +t</code>). <syntaxhighlight lang="Haskell" > 5 + 8 3 * 5 + 8 2 + 4 sqrt 16 succ 6 succ 7 pred 9 pred 8 sin (pi / 2) truncate pi round 3.5 round 3.4 floor 3.7 ceiling 3.3 mod 10 3 odd 3 </syntaxhighlight> * At presentations, we have spoken about some basic types: <code> Int, Double, Bool, Char</code>. For each of previous expressions assign them the most appropriate of these basic data types. You can verify your guess by using <code>::</code>. For example, for the first expression, let's assume it is <code>Int</code>. We can cast the result to integer and get the following result. <syntaxhighlight lang="Haskell" class="myDark" > Prelude> :type (5 + 8) :: Int (5 + 8) :: Int :: Int </syntaxhighlight> If we try incorrect conversion to <code>Char</code>, we get the following result. <syntaxhighlight lang="Haskell" class="myDark" > Prelude> :type (5 + 8) :: Char <interactive>:1:2: error: * No instance for (Num Char) arising from a use of `+' * In the expression: (5 + 8) :: Char </syntaxhighlight> For this expression, also the type <code>Double</code> works. <syntaxhighlight lang="Haskell" class="myDark" > Prelude> :type (5 + 8) :: Double (5 + 8) :: Double :: Double </syntaxhighlight> == Reasoning about types == For following expression, try to determine: *if the expression's type is correct; *what will be the type of the result; *what will be the result; *put the expression into the interpreter, and verify your claims. <syntaxhighlight lang="Haskell">5.9/7 (floor 5.9)/7 floor 5.9/7 fromIntegral floor 5.9/7 fromIntegral (floor 5.9)/7 div (floor 5.9) 7 (floor 5.9) div 7 (floor 5.9) `div` 7 mod 10/2 3 mod (floor (10/2)) 3 </syntaxhighlight> == Simple functions == Implement following functions: * Function that computes a factorial of a given number. <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/WHkBFQIHmsw]]</div> <syntaxhighlight lang="Haskell">factorial :: Int -> Int</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> factorial 5 120 </syntaxhighlight> <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> <syntaxhighlight lang="Haskell"> factorial :: Int -> Int factorial 0 = 1 factorial n = n * factorial (n-1) factorial' :: Int -> Int factorial' n | n==0 = 1 | otherwise = n * factorial'' (n-1) factorial'' :: Int -> Int factorial'' n = if n==0 then 1 else n * factorial'' (n-1) </syntaxhighlight> [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/WRUF28416]] </div> <div style="clear:both"></div> * Function that computes n-th number in Fibonacci sequence. <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/GBxb_cFQG14]]</div> <syntaxhighlight lang="Haskell">fib :: Int -> Int</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> fib 5 8 </syntaxhighlight> <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> <syntaxhighlight lang="Haskell"> fib :: Int->Int fib 0 = 1 fib 1 = 1 fib n = fib (n-1) + fib (n-2) fib' :: Int -> Int fib' n = tmp n 1 1 where tmp 0 a _ = a tmp x a b = tmp (x-1) b (a+b) </syntaxhighlight> [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/WRUF28416]] </div> <div style="clear:both"></div> * Function that checks if a year is a leap-year (divisible without remainder by 4 and it is not divisible by 100. If it is divisible by 400, it is a leap-year). <syntaxhighlight lang="Haskell">leapYear :: Int -> Bool</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> leapYear 2000 True *Main> leapYear 2020 True *Main> leapYear 2100 False *Main> leapYear 2019 False </syntaxhighlight> <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> <syntaxhighlight lang="Haskell"> leapYear :: Int -> Bool leapYear x = x `mod` 4 == 0 && x `mod` 100 /= 0 || x `mod` 400 == 0 leapYear' :: Int -> Bool leapYear' x | x `mod` 400 == 0 = True | x `mod` 100 == 0 = False | otherwise = x `mod` 4 == 0 </syntaxhighlight> [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/WRUF28416]] </div> <div style="clear:both"></div> * Implement two functions that returns a maximum from 2 respectively 3 given parameters. <syntaxhighlight lang="Haskell"> max2 :: Int -> Int -> Int max3 :: Int -> Int -> Int -> Int </syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> max2 5 8 8 *Main> max3 5 8 4 8 </syntaxhighlight> <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> <syntaxhighlight lang="Haskell"> max2 :: Int -> Int -> Int max2 x y | x >= y = x |otherwise = y max3 :: Int -> Int -> Int -> Int max3 x y z = (x `max2` y) `max2` z </syntaxhighlight> [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/WRUF28416]] </div> <div style="clear:both"></div> * Term combination is a selection of items from a collection, such that (unlike permutations) the order of elements in this selection does not matter. Compute the number of possible combinations if we are taking k things from the collection of n things. <syntaxhighlight lang="Haskell">combinations :: Int -> Int -> Int</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> combinations 8 5 56 </syntaxhighlight> <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> <syntaxhighlight lang="Haskell"> factorial :: Int -> Int factorial 0 = 1 factorial n = n * factorial (n-1) combinations :: Int -> Int -> Int combinations n k = factorial n `div` (factorial k * factorial (n-k)) combinations' :: Int -> Int -> Int combinations' n k = fromIntegral(factorial n) `div` fromIntegral(factorial k * factorial (n-k)) </syntaxhighlight> [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/WRUF28416]] </div> <div style="clear:both"></div> * Implement a function that computes the number of solutions for a quadratic equation. This quadratic equation will be given using standard coefficients: a, b, c. <syntaxhighlight lang="Haskell">numberOfRoots :: Int -> Int -> Int -> Int -- To simplify the solution, let construct can be used f x y = let a = x + y in a * a </syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> numberOfRoots 1 4 2 2 </syntaxhighlight> <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> <syntaxhighlight lang="Haskell"> numberOfRoots :: Int -> Int -> Int -> Int numberOfRoots a b c = let d = b*b - 4 * a *c in if d<0 then 0 else if d == 0 then 1 else 2 </syntaxhighlight> [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/WRUF28416]] </div> <div style="clear:both"></div> * Implement a function that computes greatest common divider for two given numbers. <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/uNs9Zqetu7k]]</div> <syntaxhighlight lang="Haskell">gcd' :: Int -> Int -> Int</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> gcd' 30 18 6 </syntaxhighlight> <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> <syntaxhighlight lang="Haskell"> gcd' :: Int -> Int -> Int gcd' a b | a > b = gcd' (a-b) b | a < b = gcd' a (b-a) | a==b = a gcd2 :: Int -> Int -> Int gcd2 a 0 = a gcd2 a b = gcd2 b (a `mod` b) </syntaxhighlight> [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/WRUF28416]] </div> <div style="clear:both"></div> * Implement a function that compute, if a given number is a prime number. <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/XSc8qjd4StQ]]</div> <syntaxhighlight lang="Haskell">isPrime :: Int -> Bool</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> isPrime 7 True </syntaxhighlight> <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> <syntaxhighlight lang="Haskell"> isPrime :: Int -> Bool isPrime 1 = False isPrime y = isPrimeTest y (y-1) where isPrimeTest _ 1 = True isPrimeTest n x | n `mod` x ==0 = False | otherwise = isPrimeTest n (x-1) </syntaxhighlight> [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/WRUF28416]] </div> <div style="clear:both"></div> == Additional exercises == * Define a function that returns true iff all three arguments are different. <syntaxhighlight lang="Haskell">threeDifferent :: Int -> Int -> Int -> Bool</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> threeDifferent 4 5 6 True *Main> threeDifferent 6 5 6 False </syntaxhighlight> * Define a function that returns true iff all four arguments are equal. <syntaxhighlight lang="Haskell">fourEqual :: Int -> Int -> Int -> Int-> Bool</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> fourEqual 4 5 6 7 False *Main> fourEqual 4 4 4 4 True </syntaxhighlight> * Define a function that computes the average of three integers. <syntaxhighlight lang="Haskell">averageThree :: Int -> Int -> Int -> Float</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> averageThree 4 10 20 11.333333 </syntaxhighlight> * Define a function that returns how many of its inputs are larger than their average value. <syntaxhighlight lang="Haskell">howManyAboveAverage :: Int -> Int -> Int -> Int</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> howManyAboveAverage 5 5 5 0 *Main> howManyAboveAverage 10 50 50 2 </syntaxhighlight> * Using the multiplication over the integer numbers, implement a recursive function that realizes the n-th power of an integer number. <syntaxhighlight lang="Haskell">power :: Int -> Int -> Int</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> power 2 3 8 *Main> power 4 2 16 *Main> power 4 3 64 </syntaxhighlight> * Using the addition over the integer numbers, implement a recursive function that realizes the multiplication of integer numbers. <syntaxhighlight lang="Haskell">mult :: Int -> Int -> Int</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> mult 4 5 20 *Main> mult 4 9 36 *Main> mult 4 0 0 </syntaxhighlight> * Define a recursive function that computes the remainder of integer division. <syntaxhighlight lang="Haskell">mod' :: Int -> Int -> Int</syntaxhighlight> <syntaxhighlight lang="Haskell" class="myDark"> *Main> mod' 40 7 5 *Main> mod' 30 5 0 *Main> mod' 20 7 6 </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
Printable version