Difference between revisions of "FP Laboratory 3"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 14: Line 14:
 
* Create a function that computes length of a list.
 
* Create a function that computes length of a list.
 
<syntaxhighlight lang="Haskell">length' :: [a] -> Int</syntaxhighlight>
 
<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">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
Line 25: Line 30:
 
* Create a function that sums the list of integers.
 
* Create a function that sums the list of integers.
 
<syntaxhighlight lang="Haskell">sumIt :: [Int] -> Int</syntaxhighlight>
 
<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">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
Line 37: Line 46:
 
* Create a function that returns the first element in the list.
 
* Create a function that returns the first element in the list.
 
<syntaxhighlight lang="Haskell">getHead :: [a] -> a</syntaxhighlight>  
 
<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">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
Line 48: Line 61:
 
* Create a function that returns the last element in the list.
 
* Create a function that returns the last element in the list.
 
<syntaxhighlight lang="Haskell">getLast :: [a] -> a</syntaxhighlight>
 
<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">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
Line 64: Line 81:
 
* Create a function that checks if an element is a member of the list.
 
* 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">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">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
Line 77: Line 98:
 
* Create a function that returns the list without the first element.
 
* Create a function that returns the list without the first element.
 
<syntaxhighlight lang="Haskell">getTail :: [a] -> [a]</syntaxhighlight>  
 
<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">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
Line 88: Line 113:
 
* Create a function that returns the list without the last element.
 
* Create a function that returns the list without the last element.
 
<syntaxhighlight lang="Haskell">getInit :: [a] -> [a]</syntaxhighlight>  
 
<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">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
Line 100: Line 129:
 
* Create a function that merge two lists into one list.
 
* Create a function that merge two lists into one list.
 
<syntaxhighlight lang="Haskell">combine :: [a] -> [a] -> [a]</syntaxhighlight>
 
<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">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
Line 112: Line 145:
 
* Create a function that finds the maximum in the list of integers.
 
* Create a function that finds the maximum in the list of integers.
 
<syntaxhighlight lang="Haskell">max' :: [Int] -> Int</syntaxhighlight>
 
<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">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
Line 131: Line 168:
 
* Create a function that reverse a list.
 
* Create a function that reverse a list.
 
<syntaxhighlight lang="Haskell">reverse' :: [a] -> [a]</syntaxhighlight>
 
<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">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
Line 148: Line 189:
 
* Create a function that product scalar multiplication if two vectors.
 
* Create a function that product scalar multiplication if two vectors.
 
<syntaxhighlight lang="Haskell">scalar :: [Int] -> [Int] -> Int</syntaxhighlight>
 
<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">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">

Revision as of 20:23, 25 September 2020

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
length' :: [a] -> Int
length' []  = 0
length' (_:xs) = 1 + length' xs
  • Create a function that sums the list of integers.
sumIt :: [Int] -> Int
*Main> sumIt [1,2,3]
6
sumIt :: [Int] -> Int
sumIt []  = 0
sumIt (x:xs) = x + sumIt xs
  • Create a function that returns the first element in the list.
getHead :: [a] -> a
*Main> getHead [1,2,3]
1
getHead :: [a] -> a
getHead (x:_) = x
  • 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]
getTail :: [a] -> [a]                   
getTail (_:xs) = xs
  • Create a function that returns the list without the last element.
getInit :: [a] -> [a]
*Main> getInit [1,2,3]
[1,2]
getInit :: [a] -> [a]
getInit [_] = []
getInit (x:xs) = x : getInit xs
  • 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]
combine :: [a] -> [a] -> [a]
combine [] y = y
combine (x:xs) y = x : combine xs y
  • 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
scalar :: [Int] -> [Int] -> Int
scalar [] [] = 0
scalar (x:xs) (y:ys) = x*y + scalar xs ys