<div dir="ltr"><div>Solving problems in languages like mercury is somewhat different than in C-like langues.</div><div><br></div><div>In a C-type language, and especially in Object Oriented Languages, you address a problem with:</div><div><br></div><div>State: Stuff I'm going to use to compute things with</div><div>Functions: Little (hopefully) chunks of steps I need to do to compute the answer</div><div>Basic Algorithm: <br></div><div> Fill up the state</div><div> Change the state with the functions until you have the answer</div><div><br></div><div>Here is a quick example:<br></div><div><br></div><div>int sum_array(int* array) {</div><div> /** Initialize the state */<br></div><div> int i = 0;</div><div> int total = 0;</div><div> int array_length = length(array);<br></div><div><br></div><div> /** Alter the state until we have the answer */<br></div><div> for(i = 0; i < array_length; i++) {</div><div> total = total + array[i];<br></div><div> }</div><div><br></div><div> /** Return the answer */</div><div> return total.<br></div><div>}<br></div><div><br></div><div><br></div><div>In functional programming, things are more like this:</div><div><br></div><div>State: Doesn't Exist. You'll probably try to habitually create some, just delete it. Everything is in the parameters and a few intermediate vars that don't change ever.<br></div><div>Functions: Relations (i.e. how the parameters are related), NOT a chunk of steps</div><div>Basic Algorithm: There isn't one, it's all just a relation.</div><div><br></div>Yep, the explanation above is totally worthless when your starting out, and the more experianced guys can probably make it a heck of a lot more accurate, but that's the best way I can explain it concisely. Here is a similar example in mercury-ish (it's late and I don't have a compiler on hand to give you a proper example right now):<div><br></div><div>
<div>**If you want a loop, you can cheat by recursing, but in a functional language this often means there is a nicer solution to be found by thinking about things a differently. Though your assignment was likely given with the explicit purpose of making you learn how to loop, so don't fret over this right now.<br></div><div><br></div><div></div>
</div><div><br></div><div>:- func sum_list(list(int)) = int.<br></div><div>sum_list([]) :- 0. %% If the list is empty, return zero.<br></div><div>sum_list([ListHead | ListTail]) :-</div><div> ListHead + sum_list(ListTail). %%Return the first element in the list added to the sum of the rest of the list. Here Tail refers to everything but the Head.<br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div>So, for your programming assignment, the easiest way to go about things is to implement a main loop recursively, as something like:</div><div><br></div><div><br></div><div><br></div><div>
%%!IO here is a clever device that makes Input and Output look like
normal parameters. This is a lie, but think of it as already containing
all the values the user will ever input, and messages that will ever be
output.
<br></div><div>main_event_loop(!IO) :- <br></div><div> Var = read_input(!IO),</div><div> handle_input(Var, !IO),</div> main_event_loop(!IO). %%In many languages this will result in stack-space exhaustion, not in Mercury.<br><div><br></div><div><br></div><div>And something similar for outputting to the screen. <br></div><div><br></div><div>On a final note, your assignment is meant to be of a particular difficulty for imperative languages. It is actually a bit more complicated to implement in a functional language because we have to write in a way that has no side-effects. You have almost certainly already been taught to solve problems by loading up state, then modifying it repeatedly. Learning a different way to solve problems can be very challenging so please try not to feel too frustrated.<br></div><div><br></div><div><br></div><div>Cheers!</div><div><br></div><div>Charles<br></div><div><br></div><br><div><div><br></div><div><br></div><div><br></div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Apr 1, 2018 at 8:13 PM, Astrid Garcia <span dir="ltr"><<a href="mailto:mellgarcia0397@gmail.com" target="_blank">mellgarcia0397@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div>One part of my project consists on taking as input a number and a string from the user. Process the number and mod it with 5 and the result will be the times I will duplicate the string that they input. I got the math part correct, but I run into problems when I try to read and store the string and to duplicate it. I am confused as to what is the equivalent on Mercury of the Java while loop, the "if (x == 1)" expression and the reading from user like the "string = input.nextLine();" in Java<br><br></div>Thank you for your help and time,<br><br></div>Astrid Garcia<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Apr 1, 2018 at 7:23 PM, Astrid Garcia <span dir="ltr"><<a href="mailto:mellgarcia0397@gmail.com" target="_blank">mellgarcia0397@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div>Hi,<br><br></div>I would appreciate help regarding reading input and using it in functions. I am working on a program in which I need to read input, store it in a variable. How can I do this? and plus how can I declare a function that takes a string, a number as input and outputs another string. I created one for mod operation "pred modnum(int::in, int::out) is det." how would the other function be constructed if it's taking in an int a string and it will output a string? <br><br></div>Thank you for your time,<br><br></div>Astrid Garcia<br></div>
</blockquote></div><br></div>
<br>______________________________<wbr>_________________<br>
users mailing list<br>
<a href="mailto:users@lists.mercurylang.org">users@lists.mercurylang.org</a><br>
<a href="https://lists.mercurylang.org/listinfo/users" rel="noreferrer" target="_blank">https://lists.mercurylang.org/<wbr>listinfo/users</a><br>
<br></blockquote></div><br></div>