From 9d6e9e87fe12c1d8c2e20ab31e07ea74c212b87b Mon Sep 17 00:00:00 2001 From: Skyball2000 Date: Sat, 22 Oct 2022 15:45:54 +0200 Subject: [PATCH] =?UTF-8?q?Aufgabe=2001=20in=203=20Ausf=C3=BChrungen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aufgabe-01/src/Aufgabe_01.elm | 31 ++++++++++ aufgabe-01/src/Aufgabe_01_2.elm | 26 ++++++++ aufgabe-01/src/Aufgabe_01_3.elm | 25 ++++++++ aufgabe-01/src/Aufgabe_01_musterloesung.elm | 68 +++++++++++++++++++++ aufgabe-01/src/Main.elm | 41 +++++++++++++ 5 files changed, 191 insertions(+) create mode 100644 aufgabe-01/src/Aufgabe_01.elm create mode 100644 aufgabe-01/src/Aufgabe_01_2.elm create mode 100644 aufgabe-01/src/Aufgabe_01_3.elm create mode 100644 aufgabe-01/src/Aufgabe_01_musterloesung.elm create mode 100644 aufgabe-01/src/Main.elm diff --git a/aufgabe-01/src/Aufgabe_01.elm b/aufgabe-01/src/Aufgabe_01.elm new file mode 100644 index 0000000..6622b72 --- /dev/null +++ b/aufgabe-01/src/Aufgabe_01.elm @@ -0,0 +1,31 @@ +module Aufgabe_01 exposing (..) + +import Html exposing (text, ul, li) +import List exposing (map) + +euro : List Int +euro = [50000, 20000, 10000, 5000, 2000, 1000, 500, 200, 100, 50, 20, 10, 2, 1] + +input : Int +input = 4711 + + +change : Int -> List Int -> Result String (List Int) +change amount currency = + if amount == 0 then Ok [] + + else + case currency of + head :: tail -> + if head < amount then + case change (amount - head) currency of + Err message -> Err message + Ok rest -> Ok (head :: rest) + + else change amount tail + [] -> Err "Kein Wechselgeld" + + +main = case change input euro of + Ok changed -> ul [] (map (\x -> li [] [(text (String.fromInt x))]) changed) + Err message -> text message diff --git a/aufgabe-01/src/Aufgabe_01_2.elm b/aufgabe-01/src/Aufgabe_01_2.elm new file mode 100644 index 0000000..f7f95bc --- /dev/null +++ b/aufgabe-01/src/Aufgabe_01_2.elm @@ -0,0 +1,26 @@ +module Aufgabe_01_2 exposing (..) + +import Html + +euro : List Int +euro = [50000, 20000, 10000, 5000, 2000, 1000, 500, 200, 100, 50, 20, 10, 2, 1] + + +wechseln : Int -> List Int -> Result String (List Int) +wechseln geldMenge waehrung = + if geldMenge <= 0 then Ok [] + else case waehrung of + head :: tail -> if head <= geldMenge + then case wechseln (geldMenge - head) waehrung of + Err msg -> Err msg + Ok wechselgeld -> Ok (head :: wechselgeld) + else wechseln geldMenge tail + [] -> Err "Kein Wechselgeld" + + +result = wechseln 4711 euro + +main = case result of + Ok wechselgeld -> Html.ul [] (List.map (\x -> Html.li [] [Html.text (String.fromInt x)]) wechselgeld) + Err msg -> Html.text msg + diff --git a/aufgabe-01/src/Aufgabe_01_3.elm b/aufgabe-01/src/Aufgabe_01_3.elm new file mode 100644 index 0000000..47f8b51 --- /dev/null +++ b/aufgabe-01/src/Aufgabe_01_3.elm @@ -0,0 +1,25 @@ +module Aufgabe_01_3 exposing (..) + +import Html + +euro : List Int +euro = [50000, 20000, 10000, 5000, 2000, 1000, 500, 200, 100, 50, 20, 10, 2, 1] + +change : Int -> List Int -> Result String (List Int) +change amount currency = + if amount <= 0 + then Ok [] + else case currency of + [] -> Err "No change available" + head :: tail -> + if head <= amount + then case change (amount - head) currency of + Ok c -> Ok (head :: c) + Err msg -> Err msg + else change amount tail + + +result = change 7563 euro +main = case result of + Err msg -> Html.text msg + Ok money -> Html.ul [] (List.map (\x -> Html.li [] [Html.text (String.fromInt x)]) money) diff --git a/aufgabe-01/src/Aufgabe_01_musterloesung.elm b/aufgabe-01/src/Aufgabe_01_musterloesung.elm new file mode 100644 index 0000000..3e64776 --- /dev/null +++ b/aufgabe-01/src/Aufgabe_01_musterloesung.elm @@ -0,0 +1,68 @@ +module Aufgabe_01 exposing (..) + +import Html exposing (text, ul, li) +import List exposing (map) + + + +type alias Amount = + Int + + +type alias Money = + List Int + + +input : Amount +input = + 4711 + + +euro : Money +euro = + [ 50000 + , 20000 + , 10000 + , 5000 + , 2000 + , 1000 + , 500 + , 200 + , 100 + , 50 + , 20 + , 10 + , 2 + , 1 + ] + + +change : Amount -> Money -> Result String Money +change amount currency = + if amount == 0 then + Ok [] + + else + case currency of + head :: tail -> + if head <= amount then + case change (amount - head) currency of + Err message -> + Err message + + Ok rest -> + Ok (head :: rest) + + else + change amount tail + + [] -> + Err "kein Wechselgeld" + + +main = case change input euro of + Ok changed -> + ul [] (map (\x -> li [] [(text (String.fromInt x))]) changed) + + Err message -> + text message diff --git a/aufgabe-01/src/Main.elm b/aufgabe-01/src/Main.elm new file mode 100644 index 0000000..18c3b08 --- /dev/null +++ b/aufgabe-01/src/Main.elm @@ -0,0 +1,41 @@ +module Main exposing (main) + +import Browser +import Html exposing (Html, button, div, text) +import Html.Events exposing (onClick) + + +-- MAIN +main = Browser.sandbox { init = init, update = update, view = view } + + + +-- MODEL +type alias Model = Int + +init : Model +init = + 0 + + +-- UPDATE +type Msg = Increment | Decrement + +update : Msg -> Model -> Model +update msg model = + case msg of + Increment -> + model + 1 + + Decrement -> + model - 1 + + +-- VIEW +view : Model -> Html Msg +view model = + div [] + [ button [ onClick Decrement ] [ text "-" ] + , div [] [ text (String.fromInt model) ] + , button [ onClick Increment ] [ text "+" ] + ]