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>
 +
<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>
 +
</div>
 +
<div style="clear:both"></div>
 +
 
* 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>
 +
 +
<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>
 +
</div>
 +
<div style="clear:both"></div>
 +
 
* 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>  
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
getHead :: [a] -> a
 +
getHead (x:_) = x
 +
</syntaxhighlight>
 +
</div>
 +
<div style="clear:both"></div>
 +
 
* 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>
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
getLast :: [a] -> a
 +
getLast (x:xs) | length xs == 0 = x
 +
              | otherwise = getLast xs
 +
</syntaxhighlight>
 +
</div>
 +
<div style="clear:both"></div>
 +
 
* 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>
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
</syntaxhighlight>
 +
</div>
 +
<div style="clear:both"></div>
 +
 
* 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>  
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
</syntaxhighlight>
 +
</div>
 +
<div style="clear:both"></div>
 +
 
* 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>  
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
</syntaxhighlight>
 +
</div>
 +
<div style="clear:both"></div>
 +
 
* 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>
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
</syntaxhighlight>
 +
</div>
 +
<div style="clear:both"></div>
 +
 
* 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>
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
</syntaxhighlight>
 +
</div>
 +
<div style="clear:both"></div>
 +
 
* 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>
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
</syntaxhighlight>
 +
</div>
 +
<div style="clear:both"></div>
 +
 
* 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>
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
</syntaxhighlight>
 +
</div>
 +
<div style="clear:both"></div>

Revision as of 08:39, 24 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
length' :: [a] -> Int
length' []  = 0
length' (_:xs) = 1 + length' xs
  • Create a function that sums the list of integers.
sumIt :: [Int] -> Int
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
getHead :: [a] -> a
getHead (x:_) = x
  • Create a function that returns the last element in the list.
getLast :: [a] -> a
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
  • Create a function that returns the list without the first element.
getTail :: [a] -> [a]
  • Create a function that returns the list without the last element.
getInit :: [a] -> [a]
  • Create a function that merge two lists into one list.
combine :: [a] -> [a] -> [a]
  • Create a function that finds the maximum in the list of integers.
max' :: [Int] -> Int
  • Create a function that reverse a list.
reverse' :: [a] -> [a]
  • Create a function that product scalar multiplication if two vectors.
scalar :: [Int] -> [Int] -> Int