Difference between revisions of "FP Laboratory 4"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 12: Line 12:
  
 
== Functions working with lists and tuples ==
 
== Functions working with lists and tuples ==
 
 
Implement following functions:
 
Implement following functions:
 
* Create a function that merge two lists into one list of tuples.
 
* Create a function that merge two lists into one list of tuples.
Line 20: Line 19:
 
* Create a function that computes n-th number in the Fibonacci sequence. The function should be use n bigger then 50 and get the result in less then a second).  
 
* Create a function that computes n-th number in the Fibonacci sequence. The function should be use n bigger then 50 and get the result in less then a second).  
 
<syntaxhighlight lang="Haskell">fibonacci :: Int -> Int</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">fibonacci :: Int -> Int</syntaxhighlight>
* Create a function that count the number of occurrences of all characters from a given string.
+
 
<syntaxhighlight lang="Haskell">countThem :: String -> [(Char, Int)]</syntaxhighlight>
+
== List comprehension ==
<syntaxhighlight lang="Haskell" class="myDark">
+
Using the list comprehension implement following functions:
*Main>countThem "hello hello hello"
 
[('h',3),('e',3),('l',6),('o',3),(' ',2)]
 
</syntaxhighlight>
 

Revision as of 13:28, 17 September 2019

Functions working with lists

Implement following functions:

  • Create a function that takes first n elements of the list.
take :: a -> [a] -> [a]
  • Create a function that takes the remaining list after the first n elements.
drop :: a -> [a] -> a
  • Create a function that find the smallest element in the list. Consider input restrictions.
minimum :: [a] -> a -- Is this right?
  • Find all prime divisors of a given number.
divisors :: a -> [a]

Functions working with lists and tuples

Implement following functions:

  • Create a function that merge two lists into one list of tuples.
zipThem:: [a] -> [b] -> [(a,b)]
  • Create a function that compute Cartesian product of two vectors.
dotProduct :: [a] -> [b] -> [(a,b)]
  • Create a function that computes n-th number in the Fibonacci sequence. The function should be use n bigger then 50 and get the result in less then a second).
fibonacci :: Int -> Int

List comprehension

Using the list comprehension implement following functions: