added 4 & 5
parent
654be3ee5b
commit
482042ce88
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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" []
|
Loading…
Reference in New Issue