Skip to content

Instantly share code, notes, and snippets.

@gregnwosu
Last active July 20, 2016 14:33
Show Gist options
  • Save gregnwosu/ef6fefa42bf2862d57df301aead022fb to your computer and use it in GitHub Desktop.
Save gregnwosu/ef6fefa42bf2862d57df301aead022fb to your computer and use it in GitHub Desktop.
why doesnt my traversable pass its tests
module ChapterExercises where
import Data.Monoid ((<>))
data List a =
Nil | Cons a (List a) deriving (Eq, Show, Ord)
instance Functor List where
fmap _ Nil = Nil
fmap f (Cons a l) = Cons (f a) (f<$>l)
instance Foldable List where
foldr _ z Nil = z
foldr f z (Cons a l) = f a $ foldr f z l
instance Applicative List where
pure a = Cons a Nil
Nil <*> _ = Nil
_ <*> Nil = Nil
(Cons f fs) <*> (Cons x xs) = Cons (f x) ((pure f <*> xs) <> (fs <*> pure x))
instance Monoid (List a) where
mempty = Nil
x `mappend` Nil = x
Nil `mappend` x = x
Cons a as `mappend` l = Cons a( as `mappend` l)
instance Traversable List where
traverse f Nil = pure mempty
traverse f (Cons x xs) = let a = Cons <$> f x
b = traverse f xs
in a <*> b
module Spec where
import Test.QuickCheck.Checkers
import Test.QuickCheck.Classes
import Test.QuickCheck
import Test.Hspec
import ChapterExercises
instance Arbitrary a => Arbitrary (List a) where
arbitrary = frequency [(3, Cons <$> arbitrary <*> arbitrary), (1, return Nil)]
list = undefined :: List (Int, Int , [Int])
take' :: Int -> List a -> List a
take' 0 _ = Nil
take' _ Nil = Nil
take' n (Cons h t) = Cons h $ take' (n - 1) t
instance Eq a => EqProp (List a) where
xs =-= ys = xs' `eq` ys'
where xs' = take' 300 xs
ys' = take' 300 ys
main :: IO()
main = do
quickBatch $ functor list
quickBatch $ monoid list
quickBatch $ applicative list
quickBatch $ traversable list
name: chapter21
version: 0.1.0.0
synopsis: Initial project template from stack
description: Please see README.md
homepage: https://github.com/githubuser/chapter21#readme
license: BSD3
license-file: LICENSE
author: Author name here
maintainer: [email protected]
copyright: 2016 Author name here
category: Web
build-type: Simple
-- extra-source-files:
cabal-version: >=1.10
library
hs-source-dirs: src
exposed-modules: Lib, ChapterExercises
build-depends: base >= 4.7 && < 5,
bytestring >= 0.10.6.0,
transformers, checkers, QuickCheck, hspec
default-language: Haskell2010
executable chapter21-exe
hs-source-dirs: app
main-is: Main.hs
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends: base
, bytestring >= 0.10.6.0
, chapter21
, wreq ,hspec, checkers
default-language: Haskell2010
test-suite chapter21-test
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Spec.hs
build-depends: base >= 4.7 && < 5,
bytestring >= 0.10.6.0,
transformers, checkers, hspec, chapter21
ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010
source-repository head
type: git
location: https://github.com/githubuser/chapter21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment