Aufgabe 01 in 3 Ausführungen

main
Skyball2000 2022-10-22 15:45:54 +02:00
parent f23b6369f6
commit 9d6e9e87fe
5 changed files with 191 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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 "+" ]
]