Skip to content

Instantly share code, notes, and snippets.

@tiagoad
Created January 23, 2018 18:06
Show Gist options
  • Save tiagoad/33868c4b17c8821d2779d38407034e4e to your computer and use it in GitHub Desktop.
Save tiagoad/33868c4b17c8821d2779d38407034e4e to your computer and use it in GitHub Desktop.
Exames de Princípios de Programação - FCUL
import Test.QuickCheck
-- Grupo 1
-- a)
encontra :: (a -> Bool) -> [a] -> Maybe a
encontra f xs
| length matches == 0 = Nothing
| otherwise = Just (head matches)
where
matches = filter f xs
-- b)
fatias :: Int -> [a] -> [[a]]
fatias n xs
| (length xs) < n = [xs]
| otherwise = (take n xs) : fatias n (drop n xs)
-- c)
separa :: Eq a => [a] -> [a] -> [[a]]
separa sep [] = []
separa sep str = left : separa sep right
where
ok = (flip notElem) sep
partes = span ok str
left = fst partes
right = dropWhile (not . ok) (snd partes)
-- Grupo 2
data Doc = Vazio
| Texto String
| NovaLinha
| Concat Doc Doc
-- a)
instance Show Doc where
show Vazio = ""
show NovaLinha = "\n"
show (Texto str) = str
show (Concat a b) = show a ++ show b
-- b)
instance Eq Doc where
a == b = (show a) == (show b)
-- Grupo 3
-- a)
instance Arbitrary Doc where
arbitrary = oneof [
return Vazio,
return NovaLinha,
do
t <- arbitrary :: Gen String
return $ Texto t,
do
a <- arbitrary
b <- arbitrary
return $ Concat a b]
-- b)
prop_concat_vazio doc =
Concat doc Vazio == doc &&
Concat Vazio doc == doc
prop_concat_assoc a b c=
(Concat (Concat a b) c) == (Concat a (Concat b c))
-- Grupo 4
???
-- Grupo 5 (Java)
Stream.iterate(0, x -> x + 1).limit(n)
.filter(x -> x%2 == 1)
.map(x -> x * x)
.reduce(0, (a, b) -> a + b);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment