Pure (programming language)
≈ 4 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
Pure, successor to the equational language Q, is a dynamically typed, functional programming language based on term rewriting. It has facilities for user-defined operator syntax, macros, arbitrary-precision arithmetic (multiple-precision numbers), and compiling to native code through LLVM. Pure is free and open-source software distributed (mostly) under the GNU Lesser General Public License version 3 or later.
Pure, successor to the equational language Q, is a dynamically typed, functional programming language based on term rewriting. It has facilities for user-defined operator syntax, macros, arbitrary-precision arithmetic (multiple-precision numbers), and compiling to native code through LLVM. Pure is free and open-source software distributed (mostly) under the GNU Lesser General Public License version 3 or later. ==Examples== The Fibonacci numbers (naive version): fib 0 = 0; fib 1 = 1; fib n = fib (n-2) + fib (n-1) if n>1; Better (tail-recursive and linear-time) version: fib n = fibs (0,1) n with fibs (a,b) n = if n Compute the first 20 Fibonacci numbers: map fib (1..20); An algorithm for the n queens problem which employs a list comprehension to organize the backtracking search: queens n = search n 1 [] with search n i p = [reverse p] if i>n; safe (i,j) p = ~any (check (i,j)) p; check (i1,j1) (i2,j2) = i1==i2 || j1==j2 || i1+j1==i2+j2 || i1-j1==i2-j2; end; While Pure uses eager evaluation by default, it also supports lazy data structures such as streams (lazy lists). For instance, David Turner's algorithm for computing the stream of prime numbers by trial division can be expressed in Pure: primes = sieve (2..inf) with sieve (p:qs) = p : sieve [q | q = qs; q mod p] &; end; Use of the & operator turns the tail of the sieve into a thunk to delay its computation. The thunk is evaluated implicitly and then memoized (using call by need evaluation) when the corresponding part of the list is accessed, e.g.: primes!!(0..99); // yields the first 100 primes Pure has efficient support for vectors and matrices (similar to that of MATLAB and GNU Octave), including vector and matrix comprehensions. Namespaces, types and interfaces belong to the standard repertoire: nonfix nil; type bintree nil | bintree (bin x left right); outfix « »; namespace foo (« »); infixr (::^) ^; x^y = 2*x+y; namespace; interface stack with push s::stack x; pop s::stack; top s::stack; end; type stack []; push xs@[] x | push xs@(_:_) x = x:xs; pop (x:xs) = xs; top (x:xs) = x; As a language based on term rewriting, Pure fully supports symbolic computation with expressions. Here is an example showing the use of local rewriting rules to expand and factor simple arithmetic expressions: expand = reduce with (a+b)*c = a*c+b*c; a*(b+c) = a*b+a*c; end; factor = reduce with a*c+b*c = (a+b)*c; a*b+a*c = a*(b+c); end; expand ((a+b)*2); // yields a*2+b*2 factor (a*2+b*2); // yields (a+b)*2 Calling C functions from Pure is very easy. E.g., for a "Hello, World!" program, the following imports the puts function from the C library and uses it to print the string "Hello, world!" on the terminal: extern int puts(char*); hello = puts "Hello, world!"; hello; Instead of manually compiling source files to LLVM bitcode modules, one can also place the source code into a Pure script, enclosing it in % (inline code, e.g. C, Fortran 77/90 and so on). ==See also== Functional programming Functional languages Clean (programming language) ==External links== Pure at Github The Pure Manual (html) The Pure Library Manual (html) Documentation Overview (html) Computer Algebra with Pure: A Reduce Interface Pure quick reference Using the Pure Interpreter Using Pure with TeXmacs The Pure TeXmacs Plugin
Text derived from https://en.wikipedia.org/w/index.php?oldid=1363208833 under CC BY-SA 4.0; modifications have been made.