From 482042ce8858100966a99542aa6341cf6c75b16f Mon Sep 17 00:00:00 2001 From: 1925458 <1925458tud.hs-mannheim.de> Date: Wed, 5 Feb 2025 23:32:11 +0100 Subject: [PATCH 1/2] added 4 & 5 --- .../doubleAndIncrement.hs | 17 +++++++++++ exercises/block5/2-multiplyBy/multiplyBy.hs | 20 +++++++++++++ .../3-addThreeNumbers/addThreeNumbers.hs | 22 +++++++++++++++ exercises/block5/4-bonusMonaden/bind.hs | 21 ++++++++++++++ exercises/block5/5-bonusDOM/dom.hs | 28 +++++++++++++++++++ 5 files changed, 108 insertions(+) create mode 100644 exercises/block5/1-doubleAndIncrement/doubleAndIncrement.hs create mode 100644 exercises/block5/2-multiplyBy/multiplyBy.hs create mode 100644 exercises/block5/3-addThreeNumbers/addThreeNumbers.hs create mode 100644 exercises/block5/4-bonusMonaden/bind.hs create mode 100644 exercises/block5/5-bonusDOM/dom.hs diff --git a/exercises/block5/1-doubleAndIncrement/doubleAndIncrement.hs b/exercises/block5/1-doubleAndIncrement/doubleAndIncrement.hs new file mode 100644 index 0000000..34fe2c8 --- /dev/null +++ b/exercises/block5/1-doubleAndIncrement/doubleAndIncrement.hs @@ -0,0 +1,17 @@ +doubleAndIncrement :: Int -> Int + +-- +-- AUFGABE BEGINNT HIER +-- + +doubleAndIncrement = + +-- +-- AUFGABE ENDET HIER +-- + + +-- Test cases +main = do + print (doubleAndIncrement 3) -- 7 + print (doubleAndIncrement 5) -- 11 \ No newline at end of file diff --git a/exercises/block5/2-multiplyBy/multiplyBy.hs b/exercises/block5/2-multiplyBy/multiplyBy.hs new file mode 100644 index 0000000..2641ea2 --- /dev/null +++ b/exercises/block5/2-multiplyBy/multiplyBy.hs @@ -0,0 +1,20 @@ +multiplyBy :: Int -> (Int -> Int) +double :: Int -> Int + +-- +-- AUFGABE BEGINNT HIER +-- + +multiplyBy = -- Fill in +double = -- Fill in using partial application +triple = -- Fill in using partial application + +-- +-- AUFGABE ENDET HIER +-- + +-- Test cases +main = do + print (double 4) -- Expected output: 8 + print (double 7) -- Expected output: 14 + print (triple 4) -- Expected output: 12 diff --git a/exercises/block5/3-addThreeNumbers/addThreeNumbers.hs b/exercises/block5/3-addThreeNumbers/addThreeNumbers.hs new file mode 100644 index 0000000..f2ac3dc --- /dev/null +++ b/exercises/block5/3-addThreeNumbers/addThreeNumbers.hs @@ -0,0 +1,22 @@ +addThreeNumbers :: Int -> Int -> Int -> Int +addFive :: Int -> Int -> Int + +-- +-- AUFGABE BEGINNT HIER +-- + +addThreeNumbers = -- Fill in +addFive = -- Fill in using partial application + +-- +-- AUFGABE ENDET HIER +-- + + + +-- Test cases +main = do + print (addThreeNumbers 3 2 4) -- 9 + print (addThreeNumbers 7 6 4) -- 17 + print (addFive 3 2) -- 10 + print (addFive 1 4) -- 10 diff --git a/exercises/block5/4-bonusMonaden/bind.hs b/exercises/block5/4-bonusMonaden/bind.hs new file mode 100644 index 0000000..75a9f6e --- /dev/null +++ b/exercises/block5/4-bonusMonaden/bind.hs @@ -0,0 +1,21 @@ +module Main where + +-- Definiert einen simplen container für unsere Werte +data Box a = Box a deriving (Show, Eq) + +-- Schreibe die Unit funktion, welche einen Wert in einen Container verschachtelt! +unit :: a -> Box a + + +-- Schreibe die Bind Funktion, welches es einem Erlaubt +bind :: Box a -> (a -> Box b) -> Box b + + +-- Schreibe eine Lift Funktion, welche eine Funtion nimmt und diesselbe funktion mit den Monadischen Werten als Eigabe und Ausgabetyp besitzt +lift :: (a -> b) -> Box a -> Box b + + +-- Main Function to run tests +main :: IO () +main = do + print $ unit 10 -- Box 10 \ No newline at end of file diff --git a/exercises/block5/5-bonusDOM/dom.hs b/exercises/block5/5-bonusDOM/dom.hs new file mode 100644 index 0000000..b385525 --- /dev/null +++ b/exercises/block5/5-bonusDOM/dom.hs @@ -0,0 +1,28 @@ +-- Haskell Exercise: Implementing Bind, Unit, and Lift for a DOM-like Structure + +module Main where + +-- Define a simple data type for DOM Elements +data DOM a = Element a [DOM a] | EmptyElement deriving (Show, Eq) + +-- Unit function: Wraps a value in the DOM type +unit :: a -> DOM a +unit x = Element x [] + +-- Bind function: Chains computations on DOM elements +bind :: DOM a -> (a -> DOM b) -> DOM b +bind EmptyElement _ = EmptyElement +bind (Element x children) f = + case f x of + EmptyElement -> EmptyElement + Element newX newChildren -> Element newX (newChildren ++ map (`bind` f) children) + +-- Lift function: Lifts a normal function into the DOM context +lift :: (a -> b) -> DOM a -> DOM b +lift _ EmptyElement = EmptyElement +lift f (Element x children) = Element (f x) (map (lift f) children) + +-- Main Function to run tests +main :: IO () +main = do + print $ unit "root" -- Should print Element "root" [] -- 2.43.0 From 2de11701a8f8cc9b95f0af01b44580279a4ec57f Mon Sep 17 00:00:00 2001 From: 1925458 <1925458tud.hs-mannheim.de> Date: Wed, 5 Feb 2025 23:41:39 +0100 Subject: [PATCH 2/2] removed a mistake --- exercises/block5/5-bonusDOM/dom.hs | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/exercises/block5/5-bonusDOM/dom.hs b/exercises/block5/5-bonusDOM/dom.hs index b385525..e5642cd 100644 --- a/exercises/block5/5-bonusDOM/dom.hs +++ b/exercises/block5/5-bonusDOM/dom.hs @@ -1,28 +1,21 @@ --- Haskell Exercise: Implementing Bind, Unit, and Lift for a DOM-like Structure - module Main where --- Define a simple data type for DOM Elements +-- Definiere eine einfache Datenstruktur für DOM-Elemente data DOM a = Element a [DOM a] | EmptyElement deriving (Show, Eq) --- Unit function: Wraps a value in the DOM type +-- Schreibe die Unit funktion, welche einen Wert in einen DOM-Container verschachtelt! unit :: a -> DOM a -unit x = Element x [] --- Bind function: Chains computations on DOM elements + +-- Schreibe die Bind Funktion, welches es einem Erlaubt funktionen des Monadischen Typen DOM zu verketten bind :: DOM a -> (a -> DOM b) -> DOM b -bind EmptyElement _ = EmptyElement -bind (Element x children) f = - case f x of - EmptyElement -> EmptyElement - Element newX newChildren -> Element newX (newChildren ++ map (`bind` f) children) --- Lift function: Lifts a normal function into the DOM context + +-- Schreibe eine Lift Funktion, welche eine Funtion nimmt und diesselbe funktion mit den Monadischen Werten DOM als Eigabe und Ausgabetyp besitzt lift :: (a -> b) -> DOM a -> DOM b -lift _ EmptyElement = EmptyElement -lift f (Element x children) = Element (f x) (map (lift f) children) --- Main Function to run tests + +-- Hauptfunktion zum Ausführen von Tests main :: IO () main = do - print $ unit "root" -- Should print Element "root" [] + print $ unit "root" -- Element "root" [] \ No newline at end of file -- 2.43.0