I'm new to Erlang.
My problem though is I can't seem to figure out how to create functions that will continue to prompt a user until they enter some kind of "exit" button.
I made a very simple program below that I thought would exit if the user typed "4", but I thought wrong:
reprompt() ->
{ok, X} = io:fread("Prompt>", "~s"),
case X of
4 -> io:format("exit");
_ -> reprompt()
end.
I've tried other variations, but most of followed the same or similar pattern.
Any suggestions?
io:fread/2 returns a list. With the format "~s", it will return a list containing one string. If you want to input a single number into X, you can do:
{ok, [X]} = io:fread("Prompt>", "~d"),
Note that this will crash if the user enters something other than a number. You should use a case expression and add clauses for {ok, [X]} and {error, Reason}.
Related
val SOME i = Int.fromString e
I have a line like this on my code and smlnj shows me this warning
vm.sml:84.7-84.32 Warning: binding not exhaustive
SOME i = ...
Is this bad practice? Should I use a function to handle the option or am I missing something?
If you're just working on a small script you'll run once, it's not necessarily bad practice: If Int.fromString e fails (and returns NONE instead of SOME _), then the value binding will fail and an exception will be raised to the appropriate handler (or the program will exit, if ther is no handler). To disable this warning, you can run the top-level statement (for SML-NJ 110.96): Control.MC.bindNonExhaustiveWarn := false;.
As an alternative approach, you could throw a custom exception:
val i =
case Int.fromString e
of SOME i => i
| NONE => raise Fail ("Expected string value to be parseable as an int; got: " ^ e)
The exception message should be written in a way that's appropriate to the provenance of the e value. (If e comes from command-line input, the program should tell the user that a number was expected there; if e comes from a file, the program should tell the user which file is formatted incorrectly and where the formatting error was found.)
As yet another alternative: If your program is meant to be long-running and builds up a lot of state, it wouldn't be very user-friendly if the program crashed as soon as the user entered an ill-formed string on the command line. (The user would be quite sad in this case, as all the state they built up in the program would have been lost.) In this case, you could repeatedly read from stdin until the user types in input that can be parsed as an int. This is incidentally more-or-less what the SML/NJ REPL does: instead of something like val SOME parsedProgram = SMLofNJ.parse (getUserInput ()), it would want to do something like:
fun getNextParsedProgram () =
case SMLofNJ.parse (getUserInput ())
of NONE => (print "ERROR: failed to parse\n"; getNextParsedProgram ())
| SOME parsedProgram => parsedProgram
In summary,
For a short-lived script or a script you don't intend on running often, turning off the warning is a fine option.
For a program where it's unexpected that e would be an unparseable string, you could raise a custom exception that explains what went wrong and how the user can fix it.
For longer-lived programs where better error handling is desired, you should respect the NONE case by pattern-matching on the result of fromString, which forces you to come up with some sort of error-handling behavior.
I am attempting to send a message from one process that I spawned to another for an assignment, I feel like I am very close here, but I think my syntax is just a bit off:
-module(assignment6).
-export([start/1, process1/2, process2/0, send_message/2]).
process1(N, Pid) ->
Message = "This is the original Message",
if
N == 1 ->
timer:sleep(3000),
send_message(Pid, Message);
N > 1 ->
timer:sleep(3000),
send_message(Pid, Message),
process1(N-1, Pid);
true ->
io:fwrite("Negative/0, Int/Floating-Point Numbers not allowed")
end.
process2() ->
recieve
Message ->
io:fwrite(Message),
io:fwrite("~n");
end.
send_message(Pid, Message) ->
Pid ! {Message}.
start(N) ->
Pid = spawn(assignment6, process2, []),
spawn(assignment6, process1, [N, Pid]).
The goal of this program is that the Message, will be printed out N times when the function is started, but be delayed enough so that I can hot-swap the wording of the message mid-run. I just can't quite get the Message to process2 for printout.
Four small things:
It's spelled receive, not recieve
Remove the semicolon in process2. The last clause in a receive expression does not have a terminating semicolon. You can see this in the if expression in process1: the first two clauses end with a semicolon, but the third one does not.
In process2, print the message like this:
io:fwrite("~p~n", [Message])
Since Message is a tuple, not a string, passing it as the first argument to io:fwrite causes a badarg error. Let's ask io:fwrite to format it for us instead.
process2 should probably call itself after printing the message. Otherwise, it will receive one message and then exit.
So now you can run the code, and while it's running you can load a new version of the module with a different message (so called "hot code swapping"). Will that change the message being printed? Why / why not?
It won't. process1 does a local call to itself, which means that it stays in the old version of the module. Do an external call instead (explicitly specifying the module: assignment6:process1(N-1, Pid)), and it will switch to the new version.
I'm quite new to Erlang (Reading through "Software for a Concurrent World"). From what I've read, we link two processes together to form a reliable system.
But if we need more than two process, I think we should connect them in a ring. Although this is slightly tangential to my actual question, please let me know if this is incorrect.
Given a list of PIDs:
[1,2,3,4,5]
I want to form these in a ring of {My_Pid, Linked_Pid} tuples:
[{1,2},{2,3},{3,4},{4,5},{5,1}]
I have trouble creating an elegant solution that adds the final {5,1} tuple.
Here is my attempt:
% linkedPairs takes [1,2,3] and returns [{1,2},{2,3}]
linkedPairs([]) -> [];
linkedPairs([_]) -> [];
linkedPairs([X1,X2|Xs]) -> [{X1, X2} | linkedPairs([X2|Xs])].
% joinLinks takes [{1,2},{2,3}] and returns [{1,2},{2,3},{3,1}]
joinLinks([{A, _}|_]=P) ->
{X, Y} = lists:last(P)
P ++ [{Y, A}].
% makeRing takes [1,2,3] and returns [{1,2},{2,3},{3,1}]
makeRing(PIDs) -> joinLinks(linkedPairs(PIDs)).
I cringe when looking at my joinLinks function - list:last is slow (I think), and it doesn't look very "functional".
Is there a better, more idiomatic solution to this?
If other functional programmers (non-Erlang) stumble upon this, please post your solution - the concepts are the same.
Use lists:zip with the original list and its 'rotated' version:
1> L=[1,2,3].
[1,2,3]
2> lists:zip(L, tl(L) ++ [hd(L)]).
[{1,2},{2,3},{3,1}]
If you are manipulating long lists, you can avoid the creation of the intermediary list tl(L) ++ [hd(L)] using an helper function:
1> L = lists:seq(1,5).
[1,2,3,4,5]
2> Link = fun Link([Last],First,Acc) -> lists:reverse([{Last,First}|Acc]);
Link([X|T],First,Acc) -> Link(T,First,[{X,hd(T)}|Acc]) end.
#Fun<erl_eval.42.127694169>
3> Joinlinks = fun(List) -> Link(List,hd(List),[]) end.
#Fun<erl_eval.6.127694169>
4> Joinlinks(L).
[{1,2},{2,3},{3,4},{4,5},{5,1}]
5>
But if we need more than two process, I think we should connect them
in a ring.
No. For instance, suppose you want to download the text of 10 different web pages. Instead of sending a request, then waiting for the server to respond, then sending the next request, etc., you can spawn a separate process for each request. Each spawned process only needs the pid of the main process, and the main process collects the results as they come in. When a spawned process gets a reply from a server, the spawned process sends a message to the main process with the results, then terminates. The spawned processes have no reason to send messages to each other. No ring.
I would guess that it is unlikely that you will ever create a ring of processes in your erlang career.
I have trouble creating an elegant solution that adds the final {5,1} tuple.
You can create the four other processes passing them self(), which will be different for each spawned process. Then, you can create a separate branch of your create_ring() function that terminates the recursion and returns the pid of the last created process to the main process:
init(N) ->
LastPid = create_ring(....),
create_ring(0, PrevPid) -> PrevPid;
create_ring(N, PrevPid) when N > 0 ->
Pid = spawn(?MODULE, loop, [PrevPid]),
create_ring(.......).
Then, the main process can call (not spawn) the same function that is being spawned by the other processes, passing the function the last pid that was returned by the create_ring() function:
init(N) ->
LastPid = create_ring(...),
loop(LastPid).
As a result, the main process will enter into the same message loop as the other processes, and the main process will have the last pid stored in the loop parameter variable to send messages to.
In erlang, you will often find that while you are defining a function, you won't be able to do everything that you want in that function, so you need to call another function to do whatever it is that is giving you trouble, and if in the second function you find you can't do everything you need to do, then you need to call another function, etc. Applied to the ring problem above, I found that init() couldn't do everything I wanted in one function, so I defined the create_ring() function to handle part of the problem.
I have been plaing around with Erlang, and decided to try to make a directory lister. After hacking some code together I hit as road block, as the second commented line shows an error message. Literally it can't see the iterate function. I've done a bit of research here and on google. I have tried exporting the functions as well. There is something here that I am not thinking about correctly. Can someone point me in the correct direction?
-module(iterate_dir).
% exporting iterate/1 does not make it visible.
-export([start/0, iterate/1, show_files/2]).
show_files([], _) ->
ok;
show_files([Head|Tail], Path) ->
FullPath = [Path] ++ [Head],
case filelib:is_dir(FullPath) of
% function iteratate/1 undefined
true -> io:format("Dir ~s\n", [FullPath]), iteratate(FullPath);
false-> io:format("File ~s\n", [FullPath])
end,
show_files(Tail, Path).
iterate(Directory) ->
case file:list_dir(Directory) of
{ok, Files} -> show_files(Files, Directory);
{error, Reason} -> io:format("Error ~s~n", [Reason])
end.
start() ->
io:format("Running~n"),
iterate("c:\\"),
io:format("Complete~n").
The function is called "iterate", you are calling it as "iteratate"
notice the extra "at" in the middle at the call site (and comment)
I'm working on a p2p app that uses hash trees.
I am writing the hash tree construction functions (publ/4 and publ_top/4) but I can't see how to fix publ_top/4.
I try to build a tree with publ/1:
nivd:publ("file.txt").
prints hashes...
** exception error: no match of right hand side value [67324168]
in function nivd:publ_top/4
in call from nivd:publ/1
The code in question is here:
http://github.com/AndreasBWagner/nivoa/blob/886c624c116c33cc821b15d371d1090d3658f961/nivd.erl
Where do you think the problem is?
Thank You,
Andreas
Looking at your code I can see one issue that would generate that particular exception error
publ_top(_,[],Accumulated,Level) ->
%% Go through the accumulated list of hashes from the prior level
publ_top(string:len(Accumulated),Accumulated,[],Level+1);
publ_top(FullLevelLen,RestofLevel,Accumulated,Level) ->
case FullLevelLen =:= 1 of
false -> [F,S|T]=RestofLevel,
io:format("~w---~w~n",[F,S]),
publ_top(FullLevelLen,T,lists:append(Accumulated,[erlang:phash2(string:concat([F],[S]))]),Level);
true -> done
end.
In the first function declaration you match against the empty list. In the second declaration you match against a list of length (at least) 2 ([F,S|T]). What happens when FullLevelLen is different from 1 and RestOfLevel is a list of length 1? (Hint: You'll get the above error).
The error would be easier to spot if you would pattern match on the function arguments, perhaps something like:
publ_top(_,[],Accumulated,Level) ->
%% Go through the accumulated list of hashes from the prior level
publ_top(string:len(Accumulated),Accumulated,[],Level+1);
publ_top(1, _, _, _) ->
done;
publ_top(_, [F,S|T], Accumulated, Level) ->
io:format("~w---~w~n",[F,S]),
publ_top(FullLevelLen,T,lists:append(Accumulated,[erlang:phash2(string:concat([F],[S]))]),Level);
%% Missing case:
% publ_top(_, [H], Accumulated, Level) ->
% ...