Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
20
Task/Test-a-function/Haskell/test-a-function.hs
Normal file
20
Task/Test-a-function/Haskell/test-a-function.hs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import Test.QuickCheck
|
||||
|
||||
isPalindrome :: String -> Bool
|
||||
isPalindrome x = x == reverse x
|
||||
|
||||
{- There is no built-in definition of how to generate random characters;
|
||||
here we just specify ASCII characters. Generating strings then automatically
|
||||
follows from the definition of String as list of Char. -}
|
||||
instance Arbitrary Char where
|
||||
arbitrary = choose ('\32', '\127')
|
||||
|
||||
-- /------------------------- the randomly-generated parameters
|
||||
-- | /------------------ the constraint on the test values
|
||||
-- | | /- the condition which should be true
|
||||
-- v v v
|
||||
main = do
|
||||
putStr "Even palindromes: " >> quickCheck (\s -> isPalindrome (s ++ reverse s))
|
||||
putStr "Odd palindromes: " >> quickCheck (\s -> not (null s) ==> isPalindrome (s ++ (tail.reverse) s))
|
||||
putStr "Non-palindromes: " >> quickCheck (\i s -> not (null s) && 0 <= i && i < length s && i*2 /= length s
|
||||
==> not (isPalindrome (take i s ++ "•" ++ drop i s)))
|
||||
Loading…
Add table
Add a link
Reference in a new issue