aboutsummaryrefslogtreecommitdiff
path: root/Haskell-book/25/Bifunctor/src
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2025-12-11 10:28:11 +0100
committerEugen Wissner <belka@caraus.de>2025-12-11 10:28:11 +0100
commit98329e0a3dd4f78b5d815ac3896272ec70904901 (patch)
tree80f9c56cfe2ac20232358f236d32e84bd683be1b /Haskell-book/25/Bifunctor/src
parent3624c712d72d246f21d4e710cec7c11e052e0326 (diff)
downloadbook-exercises-98329e0a3dd4f78b5d815ac3896272ec70904901.tar.gz
Add remaining haskell book exercises
Diffstat (limited to 'Haskell-book/25/Bifunctor/src')
-rw-r--r--Haskell-book/25/Bifunctor/src/Bifunctor.hs68
1 files changed, 68 insertions, 0 deletions
diff --git a/Haskell-book/25/Bifunctor/src/Bifunctor.hs b/Haskell-book/25/Bifunctor/src/Bifunctor.hs
new file mode 100644
index 0000000..944bf13
--- /dev/null
+++ b/Haskell-book/25/Bifunctor/src/Bifunctor.hs
@@ -0,0 +1,68 @@
+{-# LANGUAGE NoImplicitPrelude #-}
+module Bifunctor where
+
+import Prelude (($), id, (.))
+
+class Bifunctor p where
+ {-# MINIMAL bimap | first, second #-}
+
+ 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
+
+-- 1
+data Deux a b = Deux a b
+
+instance Bifunctor Deux where
+ first f (Deux a c) = Deux (f a) c
+ second f (Deux a b) = Deux a (f b)
+
+-- 2
+data Const a b = Const a
+
+instance Bifunctor Const where
+ first f (Const a) = Const (f a)
+ second f (Const a) = Const a
+
+-- 3
+data Drei a b c = Drei a b c
+
+instance Bifunctor (Drei a) where
+ 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
+ first f (SuperDrei a b) = SuperDrei a (f b)
+ second f (SuperDrei a b) = SuperDrei a b
+
+-- 5
+data SemiDrei a b c = SemiDrei a
+
+instance Bifunctor (SemiDrei a) where
+ first f (SemiDrei a) = SemiDrei a
+ second f (SemiDrei a) = SemiDrei a
+
+-- 6
+data Quadriceps a b c d = Quadzzz a b c d
+
+instance Bifunctor (Quadriceps a b) where
+ first f (Quadzzz a b c d) = Quadzzz a b (f c) d
+ second f (Quadzzz a b c d) = Quadzzz a b c (f d)
+
+-- 7
+data Either a b = Left a | Right b
+
+instance Bifunctor Either where
+ first f (Left a) = Left $ f a
+ second f (Right b) = Right $ f b \ No newline at end of file