Difference between revisions of "FP Laboratory 3"
Jump to navigation
Jump to search
Line 25: | Line 25: | ||
length' (_:xs) = 1 + length' xs | length' (_:xs) = 1 + length' xs | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
Line 41: | Line 42: | ||
sumIt (x:xs) = x + sumIt xs | sumIt (x:xs) = x + sumIt xs | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
Line 56: | Line 58: | ||
getHead (x:_) = x | getHead (x:_) = x | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
Line 76: | Line 79: | ||
| otherwise = getLast' xs | | otherwise = getLast' xs | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
Line 93: | Line 97: | ||
| otherwise = isElement a xs | | otherwise = isElement a xs | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
Line 108: | Line 113: | ||
getTail (_:xs) = xs | getTail (_:xs) = xs | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
Line 124: | Line 130: | ||
getInit (x:xs) = x : getInit xs | getInit (x:xs) = x : getInit xs | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
Line 140: | Line 147: | ||
combine (x:xs) y = x : combine xs y | combine (x:xs) y = x : combine xs y | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
Line 163: | Line 171: | ||
|otherwise = tmp a xs | |otherwise = tmp a xs | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
Line 184: | Line 193: | ||
tmp (x:xs) ys = tmp xs (x:ys) | tmp (x:xs) ys = tmp xs (x:ys) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
Line 200: | Line 210: | ||
scalar (x:xs) (y:ys) = x*y + scalar xs ys | scalar (x:xs) (y:ys) = x*y + scalar xs ys | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | [[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/BEBLH60352]] | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> |
Revision as of 13:17, 22 September 2021
Usage of lists
Find out the results of the following operations:
[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]
Simple functions working with list
Implement following functions:
- Create a function that computes length of a list.
length' :: [a] -> Int
*Main> length' "ABCD"
4
- Create a function that sums the list of integers.
sumIt :: [Int] -> Int
*Main> sumIt [1,2,3]
6
- Create a function that returns the first element in the list.
getHead :: [a] -> a
*Main> getHead [1,2,3]
1
- Create a function that returns the last element in the list.
getLast :: [a] -> a
*Main> getLast [1,2,3]
3
getLast :: [a] -> a
getLast [x] = x
getLast (x:xs) = getLast xs
getLast' :: [a] -> a
getLast' (x:xs) | length xs == 0 = x
| otherwise = getLast' xs
- Create a function that checks if an element is a member of the list.
isElement :: Eq a => a -> [a] -> Bool
*Main> isElement 2 [1,2,3]
True
isElement :: Eq a => a -> [a] -> Bool
isElement _ [] = False
isElement a (x:xs) | a == x = True
| otherwise = isElement a xs
- Create a function that returns the list without the first element.
getTail :: [a] -> [a]
*Main> getTail [1,2,3]
[2,3]
- Create a function that returns the list without the last element.
getInit :: [a] -> [a]
*Main> getInit [1,2,3]
[1,2]
- Create a function that merge two lists into one list.
combine :: [a] -> [a] -> [a]
*Main> combine [1,2,3] [4,5]
[1,2,3,4,5]
- Create a function that finds the maximum in the list of integers.
max' :: [Int] -> Int
*Main> max' [3,1,7,5]
7
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
- Create a function that reverse a list.
reverse' :: [a] -> [a]
*Main> reverse' [3,1,7,5]
[5,7,1,3]
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)
- Create a function that product scalar multiplication if two vectors.
scalar :: [Int] -> [Int] -> Int
*Main> scalar [1,2,3] [4,5,6]
32