tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
8
Task/Power-set/Haskell/power-set-1.hs
Normal file
8
Task/Power-set/Haskell/power-set-1.hs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import Data.Set
|
||||
import Control.Monad
|
||||
|
||||
powerset :: Ord a => Set a -> Set (Set a)
|
||||
powerset = fromList . fmap fromList . listPowerset . toList
|
||||
|
||||
listPowerset :: [a] -> [[a]]
|
||||
listPowerset = filterM (const [True, False])
|
||||
2
Task/Power-set/Haskell/power-set-2.hs
Normal file
2
Task/Power-set/Haskell/power-set-2.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
powerset [] = [[]]
|
||||
powerset (head:tail) = acc ++ map (head:) acc where acc = powerset tail
|
||||
1
Task/Power-set/Haskell/power-set-3.hs
Normal file
1
Task/Power-set/Haskell/power-set-3.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
powerset = foldr (\x acc -> acc ++ map (x:) acc) [[]]
|
||||
15
Task/Power-set/Haskell/power-set-4.hs
Normal file
15
Task/Power-set/Haskell/power-set-4.hs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import qualified Data.Set as Set
|
||||
type Set=Set.Set
|
||||
unionAll :: (Ord a) => Set (Set a) -> Set a
|
||||
unionAll = Set.fold Set.union Set.empty
|
||||
|
||||
--slift is the analogue of liftA2 for sets.
|
||||
slift :: (Ord a, Ord b, Ord c) => (a->b->c) -> Set a -> Set b -> Set c
|
||||
slift f s0 s1 = unionAll (Set.map (\e->Set.map (f e) s1) s0)
|
||||
|
||||
--a -> {{},{a}}
|
||||
makeSet :: (Ord a) => a -> Set (Set a)
|
||||
makeSet = (Set.insert Set.empty) . Set.singleton.Set.singleton
|
||||
|
||||
powerSet :: (Ord a) => Set a -> Set (Set a)
|
||||
powerSet = (Set.fold (slift Set.union) (Set.singleton Set.empty)) . Set.map makeSet
|
||||
2
Task/Power-set/Haskell/power-set-5.hs
Normal file
2
Task/Power-set/Haskell/power-set-5.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Prelude Data.Set> powerSet fromList [1,2,3]
|
||||
fromList [fromList [], fromList [1], fromList [1,2], fromList [1,2,3], fromList [1,3], fromList [2], fromList [2,3], fromList [3]]
|
||||
Loading…
Add table
Add a link
Reference in a new issue