/* TPK (Trabb-Pardo-Knuth) algorithm in Picat. https://rosettacode.org/wiki/Trabb_Pardo%E2%80%93Knuth_algorithm Trabb Pardo–Knuth algorithm """ The TPK algorithm is an early example of a programming chrestomathy. It was used in Donald Knuth and Luis Trabb Pardo's Stanford tech report The Early Development of Programming Languages. [http://bitsavers.org/pdf/stanford/cs_techReports/STAN-CS-76-562_EarlyDevelPgmgLang_Aug76.pdf ] The report traces the early history of work in developing computer languages in the 1940s and 1950s, giving several translations of the algorithm. From the wikipedia entry [http://en.wikipedia.org/wiki/Trabb_Pardo%E2%80%93Knuth_algorithm]: ask for 11 numbers to be read into a sequence S reverse sequence S for each item in sequence S result := call a function to do an operation if result overflows alert user else print result The task is to implement the algorithm: * Use the function: f(x) = |x|^0.5 + 5x^3 * The overflow condition is an answer of greater than 400. * The 'user alert' should not stop processing of other items of the sequence. * Print a prompt before accepting eleven, textual, numeric inputs. * You may optionally print the item as well as its associated result, but the results must be in reverse order of input. * The sequence S may be 'implied' and so not shown explicitly. * Print and show the program in action from a typical run here. (If the output is graphical rather than text then either add a screendump or describe textually what is displayed). """ Also see the following for discussion (and an alternative implementation) * Sergii Dymchenko: "An Introduction to Tabled Logic Programming with Picat" http://www.linuxjournal.com/content/introduction-tabled-logic-programming-picat * Sergii Dymchenko and Mariia Mykhailov: "Declaratively solving Google Code Jam problems with Picat" Run as (e.g.) $ echo "1 2 3 4 5 6 7 8 9 10 11" | picat tpk.pi This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. main => go. go => L = [I.parse_term() : I in split(read_line())], println(l=L), S = [[I,to_fstring("%0.2%f",F),cond(F<=400,F,'TOO LARGE')] : I in L.len..-1..1, F=f(L[I])], println(S), nl. % as a one liner % (note: map/2 is not recommended in general but it fits nice here) go2 => println([[I,cond(F<=400,F,'TOO LARGE')] : L=read_line().split().map(parse_term),I in L.len..-1..1,F=f(L[I])]). % If we are allowed to write just f(X) instead of [X,f(X)] go3 => println([[cond(I<=400,I,'TOO LARGE')]:I in read_line().split().map(parse_term).map(f).reverse()]). % read a fixed string go4 => T = "10 -1 1 2 3 4 4.3 4.305 4.303 4.302 4.301", L = [I.parse_term() : I in split(T)], N = L.len, println(L), S = [[I,cond(F<=400,F,'TOO LARGE')] : I in N..-1..1, F=f(L[I])], println(S), nl. % random go5 => _ = random2(), N = 11, M = 1000, L = [ ((random() mod M)-M div 2)/100 : _ in 1..N], println(L), S = [[I,L[I],cond(F<=400,F,'TOO LARGE')] : I in N..-1..1, F=f(L[I])], println(S), nl. f(T) = sqrt(abs(T)) + 5*T**3.