Difference between revisions of "FP Laboratory 3"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 52: Line 52:
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
 
getLast :: [a] -> a
 
getLast :: [a] -> a
getLast (x:xs) | length xs == 0 = x
+
getLast [x] = x
              | otherwise = getLast xs
+
getLast (x:xs) = getLast xs
 +
 
 +
getLast' :: [a] -> a
 +
getLast' (x:xs) | length xs == 0 = x
 +
                | otherwise = getLast' xs
 
</syntaxhighlight>
 
</syntaxhighlight>
 
</div>
 
</div>
Line 63: Line 67:
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
 +
isElement :: Eq a => a -> [a] -> Bool
 +
isElement _ [] = False
 +
isElement a (x:xs) | a == x = True
 +
                  | otherwise = isElement a xs
 
</syntaxhighlight>
 
</syntaxhighlight>
 
</div>
 
</div>
Line 72: Line 80:
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
 +
getTail :: [a] -> [a]                 
 +
getTail (_:xs) = xs
 
</syntaxhighlight>
 
</syntaxhighlight>
 
</div>
 
</div>
Line 81: Line 91:
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
 +
getInit :: [a] -> [a]
 +
getInit [_] = []
 +
getInit (x:xs) = x : getInit xs
 
</syntaxhighlight>
 
</syntaxhighlight>
 
</div>
 
</div>
Line 90: Line 103:
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
 +
combine :: [a] -> [a] -> [a]
 +
combine [] y = y
 +
combine (x:xs) y = x : combine xs y
 
</syntaxhighlight>
 
</syntaxhighlight>
 
</div>
 
</div>
Line 99: Line 115:
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
 +
max' :: [Int] -> Int
 +
max' [x] = x
 +
max' (x:y:z) | x > y = max' (x:z)
 +
            | otherwise = max' (y:z)
 +
 +
max'' :: [Int] -> Int
 +
max'' (y:ys) = tmp y ys where
 +
  tmp a [] = a
 +
  tmp a (x:xs) | x > a = tmp x xs
 +
                |otherwise = tmp a xs
 
</syntaxhighlight>
 
</syntaxhighlight>
 
</div>
 
</div>
Line 108: Line 134:
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
 +
reverse' :: [a] -> [a]           
 +
reverse' [] = []
 +
reverse' (x:xs) = (reverse' xs) ++ [x]
 +
 +
reverse'' :: [a] -> [a]
 +
reverse'' n = tmp n  []
 +
  where tmp [] ys = ys
 +
        tmp (x:xs) ys = tmp xs (x:ys)
 
</syntaxhighlight>
 
</syntaxhighlight>
 
</div>
 
</div>
Line 117: Line 151:
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
 +
scalar :: [Int] -> [Int] -> Int
 +
scalar [] [] = 0
 +
scalar (x:xs) (y:ys) = x*y + scalar xs ys
 
</syntaxhighlight>
 
</syntaxhighlight>
 
</div>
 
</div>
 
<div style="clear:both"></div>
 
<div style="clear:both"></div>

Revision as of 08:56, 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] = x
getLast (x:xs) = getLast xs

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
isElement :: Eq a => a -> [a] -> Bool
isElement _ [] = False
isElement a (x:xs) | a == x = True 
                   | otherwise = isElement a xs
  • Create a function that returns the list without the first element.
getTail :: [a] -> [a]
getTail :: [a] -> [a]                   
getTail (_:xs) = xs
  • Create a function that returns the list without the last element.
getInit :: [a] -> [a]
getInit :: [a] -> [a]
getInit [_] = []
getInit (x:xs) = x : getInit xs
  • Create a function that merge two lists into one list.
combine :: [a] -> [a] -> [a]
combine :: [a] -> [a] -> [a]
combine [] y = y
combine (x:xs) y = x : combine xs y
  • Create a function that finds the maximum in the list of integers.
max' :: [Int] -> Int
max' :: [Int] -> Int
max' [x] = x
max' (x:y:z) | x > y = max' (x:z)
             | otherwise = max' (y:z)

max'' :: [Int] -> Int
max'' (y:ys) = tmp y ys where
   tmp a [] = a
   tmp a (x:xs) | x > a = tmp x xs 
                |otherwise = tmp a xs
  • Create a function that reverse a list.
reverse' :: [a] -> [a]
reverse' :: [a] -> [a]             
reverse' [] = []
reverse' (x:xs) = (reverse' xs) ++ [x]

reverse'' :: [a] -> [a] 
reverse'' n = tmp n  []
  where tmp [] ys = ys 
        tmp (x:xs) ys = tmp xs (x:ys)
  • Create a function that product scalar multiplication if two vectors.
scalar :: [Int] -> [Int] -> Int
scalar :: [Int] -> [Int] -> Int
scalar [] [] = 0
scalar (x:xs) (y:ys) = x*y + scalar xs ys