[erlang-questions] feedback please

Roelof Wobben r.wobben@REDACTED
Fri Sep 25 08:22:31 CEST 2015


Op 25-9-2015 om 02:58 schreef Richard A. O'Keefe:
> On 24/09/2015, at 5:56 pm, zxq9 <zxq9@REDACTED> wrote:
>
>> On Thursday 24 September 2015 07:41:40 Roelof Wobben wrote:
>>> Thanks,
>>>
>>> You are right. I did take a quick look and almost the whole chapter is
>>> try .. catch.  So match or chrash is better ?
>> It is not so much of a matter of which is "better" -- but that *most* of the time you want to follow the mantra of "let it crash" and this is a very natural fit for Erlang.
> It must be said that *this* specific example is one where
> crashing is an extremely bad thing.  If you went to your
> ATM and a process in the bank's machines had crashed and
> completely lost track of your money, you would be EXTREMELY
> unhappy.  Lawyer-level unhappy.
>
> A lot of values can be thrown away and no harm done.
> But if a process is supposed to be managing persistent
> state there may be serious legal penalties attached to
> losing it.
>
> This would make a nice little DETS example:  does it
> really make sense to keep the balance in a persistent
> store?  Too right it does!
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
> -----
> Geen virus gevonden in dit bericht.
> Gecontroleerd door AVG - www.avg.com
> Versie: 2015.0.6140 / Virusdatabase: 4419/10692 - datum van uitgifte: 09/24/15
>

Hello,

Here is my version with a quit choice and a check if the beginning 
balance is zero or more :

-module(balance).
-export([maintain_balance/1]).

maintain_balance(Balance) when Balance >= 0  ->
     io:format("~n~n"),
     io:format("Your account balance is $~w.~n",  [Balance]),
     Action = ask_action(),
     case Action  of
        quit ->
             io:format("Thanks for using our software");
         _    ->
             Amount = ask_amount(),
             case Action of
                 withdraw -> withdraw(Balance, {Action, Amount});
                 deposit -> deposit(Balance, {Action, Amount})
             end
    end;

maintain_balance(_) ->
     io:format("The balance cannot be negative").

withdraw(0, {withdraw, _}) ->
     ok = io:format("Hey! You're broke!~n"),
     maintain_balance(0);

withdraw(Balance, {withdraw, Amount}) when Amount > Balance ->
     ok = io:format("You cannot withdraw more than you have~n"),
     maintain_balance(Balance);

withdraw(Balance, {withdraw, Amount}) ->
     maintain_balance(Balance - Amount).

deposit(Balance, {deposit, Amount}) ->
     maintain_balance(Balance + Amount).

ask_action() ->
     Input = io:get_line("Which action do you need? 
(W)ithdraw/(D)eposit?/(Q)uit "),
     case Input of
         [$w | _] -> withdraw;
         [$W | _] -> withdraw;
         [$d | _] -> deposit;
         [$D | _] -> deposit;
         [$q | _] -> quit;
         [$Q | _] -> quit;
         BadInput ->
             ok = io:format("You can't seem to '~tp' today...~n"
                            "Let's try that again...~n",
                            [BadInput]),
             ask_action()
     end.

ask_amount() ->
     Input = io:get_line("How much? "),
     case scrub_amount(Input) of
         {ok, Number} when Number > 0 ->
             Number;
         {ok, 0} ->
             ok = io:format("$0? Giving up on the idea already?~n"),
             0;
         {ok, Number} ->
             ok = io:format("Hey, you! ~tp is less than no money!~n",
                            [Number]),
             ask_amount();
         {error, bad_input} ->
             ok = io:format("Hrm... '~tp' does not seem to be a number~n"
                            "Now, one more time...~n",
                            [Input]),
             ask_amount()
     end.

scrub_amount(Input) ->
     case string:to_integer(Input) of
         {error, no_integer} -> {error, bad_input};
         {Int, _}            -> {ok, Int}
     end.


Roelof





More information about the erlang-questions mailing list