Skip to content

Instantly share code, notes, and snippets.

@josejuan
Created November 9, 2019 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josejuan/c65fc1eb6bdfc19075fb6b65f7047e14 to your computer and use it in GitHub Desktop.
Save josejuan/c65fc1eb6bdfc19075fb6b65f7047e14 to your computer and use it in GitHub Desktop.
import Test.QuickCheck
import Algorithm.Search
...
-- conversion util
to :: List2 a -> [a]
to End = []
to (Elem x xs) = x: to xs
from :: [a] -> List2 a
from [] = End
from (x:xs) = Elem x (from xs)
-- generic algorithms (Foldable, Traversable, ...)
countChange :: (Ord a, Foldable f, Functor f, Num a) => f a -> a -> Maybe [a]
countChange coins target = bfs (add_one_coin `pruning` (> target)) (== target) 0
where add_one_coin amt = fmap (+ amt) coins
{-
Using lists:
> countChange [25, 10, 5, 1] 65
Just [25,50,60,65]
Using List2:
> countChange (from [25, 10, 5, 1]) 65
Just [5,15,40,65]
-}
-- as general property
countChangeProperty xs x = all (>0) xs && x > 0 ==> countChange xs x == countChange (from xs) x
{-
> quickCheck countChangeProperty
*** Failed! Falsified (after 43 tests and 13 shrinks):
[1,2]
15
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment