zipList
This commit is contained in:
parent
f348445975
commit
a7309c7473
2 changed files with 16 additions and 7 deletions
13
applicative/CustomList.hs
Normal file
13
applicative/CustomList.hs
Normal file
|
@ -0,0 +1,13 @@
|
|||
newtype CustomList a = CustomList [a] deriving Show
|
||||
|
||||
instance Functor CustomList where
|
||||
fmap f (CustomList lst) = CustomList $ fmap f lst
|
||||
|
||||
instance Applicative CustomList where
|
||||
pure x = CustomList $ repeat x
|
||||
{-
|
||||
-- default implementation
|
||||
pure x = CustomList [x]
|
||||
(CustomList fs) <*> (CustomList xs) = CustomList $ [f x | f <- fs, x <- xs]
|
||||
-}
|
||||
(CustomList fs) <*> (CustomList xs) = CustomList $ zipWith ($) fs xs
|
|
@ -16,7 +16,7 @@ u <*> (v <*> w) == pure (.) <*> u <*> v <*> w
|
|||
|
||||
## Доказательство
|
||||
|
||||
### Identity
|
||||
### 1.Identity
|
||||
|
||||
```
|
||||
pure id <*> CustomList lst ==
|
||||
|
@ -45,10 +45,6 @@ pure ($ y) <*> u ==
|
|||
CustomList $ repeat ($ y) <*> CustomList fs ==
|
||||
CustomList $ zipWith ($) (repeat ($ y)) fs [2]
|
||||
|
||||
Type of ($ y): (a -> b) -> b
|
||||
Type of ($): (a -> b) -> a -> b
|
||||
Type of ($) ($ y): (a -> b) -> b
|
||||
|
||||
($) f x == ($) ($ x) f
|
||||
|
||||
=> [1] == [2]
|
||||
|
@ -62,10 +58,10 @@ CustomList fs2 <*> (CustomList fs1 <*> CustomList xs) ==
|
|||
CustomList fs2 <*> CustomList $ zipWith ($) fs1 xs ==
|
||||
CustomList fs2 <*> CustomList xs1 ==
|
||||
... ==
|
||||
CustomList xs12 (Длина xs12 - наименьшая из длин fs1, fs2 и xs)
|
||||
CustomList xs12 (Длина xs12 - наименьшая из длин fs1, fs2 и xs, сначала применены ф-ии из fs1, потом - из fs2)
|
||||
|
||||
pure (.) <*> u <*> v <*> w ==
|
||||
pure (.) CustomList fs2 <*> CustomList fs1 <*> CustomList xs ==
|
||||
CustomList fs12 <*> Custom xs == (fs12 - список из композиций соответствующих функций, длина fs12 - наименьшая из длин fs1 и fs2)
|
||||
CustomList xs12 (длина xs12 - наименьшая из длин fs12 и xs)
|
||||
CustomList xs12 (длина xs12 - наименьшая из длин fs12 и xs, сначала применены ф-ии из fs1, потом - из fs2))
|
||||
```
|
||||
|
|
Reference in a new issue