Below is a program that writes data given in table (or matrix) format in csv file format.
The reason why I use append/3 here is to avoid fail driving, but it feels a little unnatural.
How can I improve the code to make it easier to understand?
The main reason why append/3 is used here is when all lines are finished with the last sub-target R= Because I want to end the predicate with the truth.If there is no problem, member/2 should be fine.
The one who disliked this style is the definition using append/3 above.Now, what else? Is there an alternative expression?prolog
I will comment as a questioner.
Why don't we use forall/2?I was looking forward to hearing that
You answered as expected.
I wrote it as a comment to the respondents above, but it's hard to read, so I'll rewrite it.
Code called failed drive
Use this for all/2 and
It's a refreshing rewrite.
For all (P, Q) is true in all cases where P becomes true, so is Q.That's what it means.
If P is true and Q is false, forall/2 itself fails.
Forall/2 has the above relationship between P and Q.Alternatively, it is a predicate for examining relationships
It can also be said that
However, in the above example, the predicate defining the relationship is used to express procedural conjunctions. Yes.
I like to use this expression because I think it's OK, but I have something to keep in mind.That's
In the Q-side section, there should be no cases where the code above does not reach the fail.
This is only writef/2 here, so it is obvious that this will be true and fail, but
If the code in Q is large, complicated, or contains Exception,
I suspect that there may be cases where the failure has not been reached, so I need to crush it. Yes.
You can combine forall/2 and member/2 for repeat processing.
This way of writing can honestly express the processing of write_csv_line for each member of the table Row.
Of course, people who read this cannot understand unless they know how to use forall/2.However, a failure-driven loop is not easy to read from the program how far it will backtrack after a failure, putting a strain on the program reader.
© 2023 OneMinuteCode. All rights reserved.