-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Anastasios Valtinos
committed
Oct 12, 2022
1 parent
c48f237
commit bd5ca98
Showing
3 changed files
with
94 additions
and
4 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
...ell Excercises & Code/Chapter25 - Composing Types/92b7cb37-2583-4e69-b659-1a761af1d688.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Bifa where | ||
|
||
class Bifunctor p where | ||
bimap :: (a -> b) -> (c -> d) -> p a c -> p b d | ||
bimap f g = first f . second g | ||
|
||
first :: (a -> b) -> p a c -> p b c | ||
first f = bimap f id | ||
|
||
second :: (b -> c) -> p a b -> p a c | ||
second = bimap id | ||
|
||
class Functor f where | ||
fmap :: (a->b) -> f a -> f b | ||
|
||
-- Write BiFunctor instances | ||
-- 1. | ||
data Deux a b = Deux a b | ||
|
||
instance Bifunctor Deux where | ||
bimap f g (Deux a b) = Deux $ (f a) . (g b) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
Haskell Excercises & Code/Chapter25 - Composing Types/funExer.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Bifa where | ||
|
||
class Bifunctor p where | ||
bimap :: (a -> b) -> (c -> d) -> p a c -> p b d | ||
bimap f g = first f . second g | ||
|
||
first :: (a -> b) -> p a c -> p b c | ||
first f = bimap f id | ||
|
||
second :: (b -> c) -> p a b -> p a c | ||
second = bimap id | ||
|
||
class Functor f where | ||
fmap :: (a->b) -> f a -> f b | ||
|
||
-- Write BiFunctor instances | ||
-- 1. | ||
data Deux a b = Deux a b | ||
|
||
instance Bifunctor Deux where | ||
bimap f g (Deux a b) = Deux (f a) (g b) | ||
first f (Deux a b) = Deux (f a) b | ||
second f (Deux a b) = Deux a (f b) | ||
|
||
-- 2. | ||
data Const a b = Const a | ||
|
||
instance Bifunctor Const where | ||
bimap f g (Const a) = Const (f a) | ||
first f (Const a) = Const (f a) | ||
|
||
--3.s | ||
data Drei a b c = Drei a b c | ||
|
||
instance Bifunctor (Drei a) where | ||
bimap f g (Drei a b c) = Drei a (f b) (g c) | ||
first f (Drei a b c) = Drei a (f b) c | ||
second f (Drei a b c) = Drei a b (f c) | ||
|
||
-- 4 | ||
data SuperDrei a b c = SuperDrei a b | ||
|
||
instance Bifunctor (SuperDrei a) where | ||
bimap f _ (SuperDrei a b) = SuperDrei a (f b) |