Difference between revisions of "FP Laboratory 3"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 1: Line 1:
 +
== 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 ==
 
== Simple functions working with list ==
 
 
Implement following functions:
 
Implement following functions:
 
* Create a function that computes length of a list.
 
* Create a function that computes length of a list.
Line 10: Line 20:
 
* 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>
 +
* Create a function that checks if an element is a member of the list.
 +
<syntaxhighlight lang="Haskell">isElement :: a -> [a] -> Bool</syntaxhighlight>
 
* 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>  

Revision as of 11:01, 17 September 2019

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
  • Create a function that sums the list of integers.
sumIt :: [Int] -> Int
  • Create a function that returns the first element in the list.
getHead :: [a] -> a
  • Create a function that returns the last element in the list.
getLast :: [a] -> a
  • Create a function that checks if an element is a member of the list.
isElement :: 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 reverse a list.
reverse :: [a] -> [a]
  • Create a function that merge two lists into one list.
combine :: [a] -> [a] -> [a]
  • Create a function that product scalar multiplication if two vectors.
scalar :: [Int] -> [Int] -> Int