-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathV6T1.hs
114 lines (83 loc) · 3.12 KB
/
V6T1.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
{-#LANGUAGE DeriveFunctor#-}
module V6T1 where
{-Implement the following functions with traverse f, where traverse is from Data.Traversable and f is your own creation.
tminimum :: (Traversable t, Bounded a, Ord a) => t a -> a to find the minimal element
tmap :: Traversable t => (a -> b) -> t a -> t b to consistently change all elements
tmodify :: Traversable t => (a -> a) -> t a -> t a to change the last element if it exists
-}
import Data.Traversable
import Data.Functor.Const
import Data.Semigroup
import Data.Foldable
import Data.Functor.Identity
import Control.Monad.State.Lazy
import Control.Applicative.Backwards
--let a;
--for (let i of lista) if (i<a) a=i;
--data Min a = Min a deriving (Functor, Show)
{-
instance (Ord a) => Semigroup (Min a) where
(<>) (Min a) (Min b) = Min (min a b)
instance Applicative Min where
pure = Min
(<*>) (Min f) (Min b) = Min(f b)
-}
tminimum :: (Traversable t, Bounded a, Ord a) => t a -> a
tminimum list = getMin $ getConst $ traverse fun list where
fun a = Const (Min a)
-- (Const . Min)
--Apujuttuja.
runMinList ::(Traversable t,Bounded a, Ord a) => Min (t a) -> a
runMinList (Min list) =(\(Min a) -> a) $ foldMap (\a->Min a) list
tminimum'' :: (Traversable t, Bounded a, Ord a) => t a -> a
tminimum'' list = runMinList $ traverse f list where
f = (\a->Min a)
tminimum' ::(Bounded a, Ord a )=> [a] -> a
tminimum' list = (\(Min a)->a) $ foldMap (\a->Min a) list
--traverse :: Applicative f => (a -> f b) -> t a -> f (t b)
--foldMap :: Monoid m => (a -> m) -> t a -> m
foldMaps :: (Monoid m, Traversable t) => (a -> m) -> t a -> m
foldMaps f = getConst . traverse fun where
fun a = Const (f a)
sequenceA :: [Min a] -> Min [a]
sequenceA list = Min $ map (\(Min a) -> a) list
sequenceAC :: Monoid c => [Const c a] -> Const c [a]
sequenceAC [] = Const mempty
sequenceAC (x:xs) = Const $ getConst x `mappend` ys
where ys = getConst $ sequenceAC xs
--Apujuttuja.
--consistently change all elements
tmap :: Traversable t => (a -> b) -> t a -> t b
tmap f list = runIdentity $ traverse (Identity . f) list
sequenceAT :: [Identity a] -> Identity [a]
sequenceAT [] = Identity mempty
sequenceAT (x:xs) =Identity $ runIdentity x : (runIdentity $ sequenceAT xs)
--fmap :: (a -> b) -> t a -> t b
--traverse :: Applicative f => (a -> f b) -> t a -> f (t b)
--(>>=) :: m a -> (a -> m b) -> m b
-- to change the last element if it exists
tmodify :: Traversable t => (a -> a) -> t a -> t a
tmodify f list = _1 traverse State(Bool) list where
h x = _
{-
flip evalState 0 $
h a = StateT (\s-> p s)
p s' = case s' of
() -> (a, 1)
_ -> (a, 1)-}
--StateT {runStateT :: s -> m (a, s)}
--newtype StateT s m a :: * -> (* -> *) -> * -> *
--sequenceA :: f (m a) -> m (f a)
sequenceAS :: [StateT s m ma] -> StateT s m [ma]
--sequenceAS [] = StateT (\s->Const [] )
sequenceAS (x:xs) = StateT $ execStateT x _ >>= runStateT (sequenceAS xs)
----Ei tästä tule yhtään mitään.
{-You should be able to build f from the following pieces.
Identity from base
Const c from base
Min from base
ReaderT r m from mtl
WriterT w m from mtl
StateT s m from mtl
Backwards from transformers
-}