I can easily parse strings with DCG using the Mercury programming language. It is compiled, statically typed, and fast.
I can easily parse strings with DCG using the Mercury programming language. It is compiled, statically typed, and fast.
Now I have added #Mercury --
https://rosettacode.org/wiki/Modular_arithmetic#Mercury
#RosettaCode #MercuryLang #LogicProgramming #FunctionalProgramming
(For convenience I use higher-order functions, so that the Functional Programming hashtag makes sense. Also, Mercury is a language in which tail recursion is the preferred method of looping. And, it just occurs to me, I wrote the program mostly as ‘functions’ rather than ‘predicates’!)
There is no order of evaluation, other than the order of evaluation that works.
@Absinthe How about, one crazy function that works both ways!
https://git.sr.ht/~namark/mercury_stuff/tree/master/toys/serialize_btree.m
Though, it goes haywire if input is invalid or if value contains anything in the form of "|number|", where number is less than depth of the tree. Probably has something to do with compiler complaining that code I wrote is impure and non-deterministic, and me just silencing it.
Also it only handles string values, cause I don't know how to write generic code yet.
@freemo @Absinthe
My attempt without division
https://git.sr.ht/~namark/mercury_stuff/tree/master/toys/other_product_no_div.m
It's linear but requires 3x the space. The idea is to calculate partial products of the list, from left to right, and right to left, then the final values based on corresponding neighbouring values in partial product lists.
Probably the worst explanation and my mercury code seems unreadable to me, so here's a c++ version
http://ix.io/1KzG/cpp
@Absinthe
other product using division
https://git.sr.ht/~namark/mercury_stuff/tree/master/toys/other_product.m
hate all the edge cases
no division version in the working, hopefully would be much smoother.
@Absinthe this one was actually "easy", just translated the description and mercury solved it for me (no squinting required), so I decided to throw in some input handling and that was the painful part as usual.
https://git.sr.ht/~namark/mercury_stuff/tree/master/toys/pairs_add_up_to.m
But of course the real challenge is doing it in one pass... in a language that might not have a concept of a pass...