Created
November 16, 2014 22:24
-
-
Save Dierk/9c454733e80badd761fd to your computer and use it in GitHub Desktop.
Frege template for the recursive functions
lab (section 8 "interactive programs") with quickcheck support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Lab4 where | |
------------------------------------------------------------------------------------------------------------------------------ | |
-- RECURSIVE FUNCTIONS, Frege version by Dierk Koenig | |
------------------------------------------------------------------------------------------------------------------------------ | |
-- import frege.prelude.Math(pi) -- uncomment this when trying to use pi | |
-- =================================== | |
-- Ex. 0 | |
-- =================================== | |
triangle :: Integer -> Integer | |
triangle n = undefined | |
-- =================================== | |
-- Ex. 1 | |
-- =================================== | |
count :: Eq a => a -> [a] -> Int | |
count x xs = undefined | |
xs = [1,2,35,2,3,4,8,2,9,0,5,2,8,4,9,1,9,7,3,9,2,0,5,2,7,6,92,8,3,6,1,9,2,4,8,7,1,2,8,0,4,5,2,3,6,2,3,9,8,4,7,1,4,0,1,8,4,1,2,4,56,7,2,98,3,5,28,4,0,12,4,6,8,1,9,4,8,62,3,71,0,3,8,10,2,4,7,12,9,0,3,47,1,0,23,4,8,1,20,5,7,29,3,5,68,23,5,6,3,4,98,1,0,2,3,8,1] | |
ys = map (\x -> ((x + 1) * 3) ^ 3 - 7) xs | |
poem = map unpacked | |
[ "Three Types for the Lisp-kings under the parentheses," | |
, "Seven for the Web-lords in their halls of XML," | |
, "Nine for C Developers doomed to segfault," | |
, "One for the Dark Lord on his dark throne" | |
, "In the Land of Haskell where the Monads lie." | |
, "One Type to rule them all, One Type to find them," | |
, "One Type to bring them all and in the Lambda >>= them" | |
, "In the Land of Haskell where the Monads lie." | |
] | |
-- =================================== | |
-- Ex. 2 | |
-- =================================== | |
euclid :: (Int, Int) -> Int | |
euclid (x, y) = undefined | |
-- =================================== | |
-- Ex. 3 | |
-- =================================== | |
funkyMap :: (a -> b) -> (a -> b) -> [a] -> [b] | |
funkyMap f g xs = undefined | |
-- Successively uncomment the lines below to validate your implementation | |
-- or jump to the TDD section below | |
-- main _ = do | |
-- println $ triangle 4 == 4 + 3 + 2 + 1 + 0 | |
-- println $ triangle 500 | |
-- println $ triangle (-500) | |
-- println $ triangle pi | |
-- | |
-- println $ count "Haskell" ["Java", "PHP", "Javascript", "C#"] == 0 | |
-- println $ count 'e' (unpacked "The quick brown fox jumped over the lazy dog.") == 4 | |
-- println $ count 722 ys | |
-- | |
-- -- the >>= "shove" operator on a list of lists is flattening the list one level deep | |
-- -- and feeds the function on the right with all elements of the left, e.g. | |
-- println $ ([[1,1,1], [2,2,2]] >>= id) == [1, 1, 1, 2, 2, 2] | |
-- | |
-- println $ count 101 (poem >>= (map $ (+4) . ord)) | |
-- | |
-- println $ euclid (6, 27) == 3 | |
-- println $ euclid (13404, 8832) | |
-- println $ euclid (1, 0) | |
-- | |
-- println $ funkyMap (+10) (+100) [1, 2, 3, 4, 5] == [11, 102, 13, 104, 15] | |
-- println $ funkyMap (+10) (+100) [1, 2, 3, 4] == [11, 102, 13, 104] | |
-- | |
-- println $ sum $ funkyMap (+10) (+100) ys | |
-- println $ sum $ funkyMap (\c -> if c == 'e' then 1 else 0) ord (poem >>= id) | |
-- -------------------------------------- TDD -------------------------------------------------- | |
-- As an alternative to starting the main function and observing the system output to contain "true" | |
-- only, you can also use QuickCheck for a TDD workflow. | |
-- Compile this module and start the QuickCheck tests below by calling the main class frege.tools.Quick | |
-- with the current module name as the argument. Depending on your setup that will be something along the lines | |
-- java -classpath .:fregec.jar frege.tools.Quick -v Lab4 | |
import Test.QuickCheck -- infile unit testing | |
-- verifying single properties once: | |
triangle_4 = once (triangle 4 == 4 + 3 + 2 + 1 ) | |
triangle_500 = once (triangle 500 == 0 ) -- this test will fail until you have the right answer! | |
-- verifying an invariant. Since x may be generated as a negative number, we have to filter those out | |
triangle_gauss = property (\x -> x < 0 || triangle x == x * (x + 1) `div` 2 ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment