data Suit = Spades | Hearts | Diamonds | Clubs data Rank = Numeric Int | Jack | Queen | King | Ace data Card = Card Rank Suit data Hand = Empty | Add Card Hand
data Hand = Empty | Add Card Hand
Empty
Add
Empty
Add c1 Empty
Add c2 (Add c1 Empty)
Add c3 (Add c2 (Add c1 Empty))
colour :: Suit -> Colour colour Spades = Black colour Clubs = Black colour _ = Red rank :: Card -> Rank rank (Card r s) = r handBeats :: Hand -> Card -> Bool handBeats Empty beat = False handBeats (Add c h) beat = cardBeats c beat || handBeats h beat
-Wincomple-patterns
)
[]
[3]
["Haskell","JavaScript","C","Python"]
[1,3,True,"Python"]
[1,2,3] /= [3,2,1]
data List a = Nil | Cons a (List a)
data [a] = [] | a : [a]
[]
x : xs
x
xs
type String = [Char]
type
keyword introduces a
type synonym, i.e. another name for an existing type.
"Haskell" == ['H','a','s','k','e','l','l']
length :: [a] -> Int (++) :: [a] -> [a] -> [a] concat :: [[a]] -> [a] take :: Int -> [a] -> [a] zip :: [a] -> [b] -> [(a,b)] map :: (a -> b) -> [a] -> [b] filter :: (a -> Bool) -> [a] -> [a]
and, or :: [Bool] -> Bool words, lines :: String -> [String] unwords, unlines :: [String] -> String
sum, product :: Num a => [a] -> a elem :: Eq a => a -> [a] -> Bool sort :: Ord a => [a] -> [a]
sort
import Data.List
qsort :: Ord a => [a] -> [a] qsort [] = [] qsort (x1:xs) = qsort smaller ++ [x1] ++ qsort bigger where smaller = [x | x<-xs, x<=x1] bigger = [x | x<-xs, x>x1]
prop_take_drop n xs = take n xs ++ drop n xs == xs nonprop_take_drop n xs = drop n xs ++ take n xs == xs
quickCheck prop_take_drop
+++ OK, passed 100 tests.
quickCheck nonprop_take_drop
+++ OK, passed 100 tests.
prop_take_drop :: Eq a => Int -> [a] -> Bool nonprop_take_drop :: Eq a => Int -> [a] -> Bool
quickCheck nonprop_take_drop
nonprop_take_drop :: Int -> [()] -> Bool
()
()
data () = ()
quickCheck
:set -XNoExtendedDefaultRules
default (Integer,Double)