Difference between revisions of "FP Laboratory 4"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 20: Line 20:
 
<syntaxhighlight lang="Haskell">fibonacci :: Int -> Int</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">fibonacci :: Int -> Int</syntaxhighlight>
  
== List comprehension ==
+
 
Using the list comprehension implement following functions:
+
== High-order functions ==
* Create a function that generates a list of all odd numbers in given interval.
 
<syntaxhighlight lang="Haskell">oddList :: Int -> Int -> [Int]</syntaxhighlight>
 
 
* Create a function that takes a string and converts all characters to upper case letters.
 
* Create a function that takes a string and converts all characters to upper case letters.
 
<syntaxhighlight lang="Haskell">allToUpper :: String -> String</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">allToUpper :: String -> String</syntaxhighlight>
* Create a function that removes all upper case letters from a string.
+
* Implement the <code>quicksort</code> algorithm. As a pivot use always the first element in the list. For dividing the list, use the function <code>filter</code>.
<syntaxhighlight lang="Haskell">removeAllUpper :: String -> String</syntaxhighlight>
+
<syntaxhighlight lang="Haskell">quicksort :: (Ord a) => [a] -> [a]</syntaxhighlight>
* Create functions that computes union and intersection of two sets.
+
<syntaxhighlight lang="Haskell" class="myDark">
<syntaxhighlight lang="Haskell">
+
*Main> filter (<5) [1..10]
union :: [a] -> [a] -> [a]
+
[1,2,3,4]
intersection :: [a] -> [a] -> [a]
 
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 14:09, 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


High-order functions

  • Create a function that takes a string and converts all characters to upper case letters.
allToUpper :: String -> String
  • Implement the quicksort algorithm. As a pivot use always the first element in the list. For dividing the list, use the function filter.
quicksort :: (Ord a) => [a] -> [a]
*Main> filter (<5) [1..10]
[1,2,3,4]