Difference between revisions of "FP Laboratory 4"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 4: Line 4:
 
* Create a function that takes first n elements of the list.
 
* Create a function that takes first n elements of the list.
 
<syntaxhighlight lang="Haskell">take' :: Int -> [a] -> [a]</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">take' :: Int -> [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 takes the remaining list after the first n elements.
 
* Create a function that takes the remaining list after the first n elements.
 
<syntaxhighlight lang="Haskell">drop' :: Int -> [a] -> [a]</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">drop' :: Int -> [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 find the smallest element in the list. Consider input restrictions.
 
* Create a function that find the smallest element in the list. Consider input restrictions.
 
<syntaxhighlight lang="Haskell">minimum' :: [a] -> a -- Is this right?</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">minimum' :: [a] -> a -- Is this right?</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>
 +
 
* Find all integer divisors of a given number.
 
* Find all integer divisors of a given number.
 
<syntaxhighlight lang="Haskell">divisors :: Int -> [Int]</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">divisors :: 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>
 +
  
 
== Functions working with lists and tuples ==
 
== Functions working with lists and tuples ==
Line 15: Line 43:
 
* Create a function that merge two lists into one list of tuples.
 
* Create a function that merge two lists into one list of tuples.
 
<syntaxhighlight lang="Haskell">zipThem:: [a] -> [b] -> [(a,b)]</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">zipThem:: [a] -> [b] -> [(a,b)]</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 compute Cartesian product of two vectors.
 
* Create a function that compute Cartesian product of two vectors.
 
<syntaxhighlight lang="Haskell">dotProduct :: [a] -> [b] -> [(a,b)]</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">dotProduct :: [a] -> [b] -> [(a,b)]</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 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>
  
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
</syntaxhighlight>
 +
</div>
 +
<div style="clear:both"></div>
  
 
== High-order functions ==
 
== High-order functions ==
 
* 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>
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
</syntaxhighlight>
 +
</div>
 +
<div style="clear:both"></div>
 +
 
* 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>.
 
* 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">quicksort :: (Ord a) => [a] -> [a]</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">quicksort :: (Ord a) => [a] -> [a]</syntaxhighlight>
Line 30: Line 84:
 
[1,2,3,4]
 
[1,2,3,4]
 
</syntaxhighlight>
 
</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 09:05, 24 September 2020

Functions working with lists

Implement following functions:

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


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]