GNU Prolog Array

Source: Internet
Author: User
Tags array example
The inventor of the GNU Prolog array Prolog design is a scholar at the University of Marseille, France. In order to maintain its pure language and Academic characteristics, the original Prolog has no arrays and no explicit data types. This is not necessarily a good thing for the actual needs of Using Prolog, because in many cases, the program runs too slowly. For example, arrays in C can be easily searched by subscript, such as array1 [3], array2 [5] [7. Doing the same thing is troublesome and inefficient in Prolog. For example, to retrieve the value of nth member in a one-dimensional list, you must scan N: array1 (N, N, A, [A | _]) from the first member one by one:-!. Array1 (n, m, B, [_ | T]):-array1 (n, m + 1, B, T ). if a multi-dimensional large array is retrieved, it will be unbearable. GNU Prolog designed a set of methods to process arrays, enhancing the practicability of PROLOG. The following example shows how to process arrays. 1. The first example of array creation, initialization, and subscript usage: |? -G_assign (W, g_array (3), g_read (w, x). x = g_array ([0, 0]) (|? -) Is the interpreter console prompt; g_assign, g_array, g_read is three internal predicates; (1 ). g_array (3) create an array with three members; (2 ). g_assign (W, g_array (3) the array name is W; (3 ). g_read (w, x) reads the array W to the variable X; X = g_array ([, 0]) is the display result of predicate matching, and the default value of array Initialization is 0. Example 2: |? -G_assign (w (0), 16), g_assign (w (1), 32), g_assign (w (2), 64), g_read (w, x ). X = g_array ([16, 32, 64]) assign values to members of the array W whose subscript is 0, 1, and 2 respectively, and then read the array W to the variable X. This is equivalent to: |? -G_assign (K, g_array ([16, 32, 64]), g_read (K, x). x = g_array ([16, 32, 64]) 2. determine the length of the array |? -G_assign (K, g_array (3, null), g_read (K, x), g_array_size (K, S ). S = 3x = g_array ([null, null, null]) 3. Two-dimensional array Example 1: |? -G_assign (W, g_array (2, g_array (3), g_read (w, x ). X = g_array ([g_array ([0, 0]), g_array ([0, 0]) Example 2: |? -(For (I,), for (J,), k is I * 3 + J, g_assign (w (I, j), k), fail; g_read (w, x )). X = g_array ([g_array ([0, 1, 2]), g_array ([3, 4, 5]) Example 3: |? -G_read (w (1), x). x = g_array ([3, 4, 5]) 4. Mixed Member array: Example 1: |? -G_assign (W, g_array ([1, 2, g_array ([a, B, c]), g_array (2, Z), 5]), g_read (w, x ). X = g_array ([1, 2, g_array ([a, B, c]), g_array ([Z, Z]), 5]) Example 2: |? -G_read (w (1), x), g_read (w (2, 1), Y), g_read (w (3, 1), Z ). X = 2y = Bz = z Example 3: |? -G_read (w (1, 2), X ). uncaught exception: Error (domain_error (g_array_index, w (), g_read/2) 5. Example 1: |? -G_assign (A, g_array ([10, 20, 30]), g_read (A, x). x = g_array ([10, 20, 30]) Example 2: |? -G_assign (A, g_array_extend (5, null), g_read (A, x). x = g_array ([10, 20, 30, null, null]) Example 3: |? -G_assign (A, g_array ([10, 20, 30]), g_read (A, x). x = g_array ([10, 20, 30]) Example 4: |? -G_assign (A, g_array_extend ([1, 2, 3, 4, 5, 6]), g_read (A, X ). X = g_array ([,]) 6. Example 1 of array automation: |? -G_assign (T, g_array_auto (3), g_assign (T (1), foo), g_read (t, x ). X = g_array ([0, Foo, 0]) Example 2: |? -G_assign (T (5), bar), g_read (t, x ). X = g_array ([0, Foo, 0, bar,]) Example 3: |? -G_assign (T, g_array_auto (2, g_array (2), g_assign (T (1, 1), foo), g_read (t, x ). X = g_array ([g_array ([0, 0]), g_array ([0, foo]) Example 4: |? -G_assign (T (3, 0), bar), g_read (t, x ). X = g_array ([g_array ([0, 0]), g_array ([0, foo]), g_array ([0, 0]), g_array ([Bar, 0]) example 5: |? -G_assign (T (3, 4), bar), g_read (t, x ). uncaught exception: Error (domain_error (g_array_index, T (3, 4), g_assign/2) Example 6: |? -G_assign (T, g_array_auto (2, g_array_auto (2), g_assign (T (1, 1), foo), g_read (t, x ). X = g_array ([g_array ([0, 0]), g_array ([0, foo]) Example 7: |? -G_assign (T (3, 3), bar), g_read (t, x ). X = g_array ([g_array ([]), g_array ([0, foo]), g_array ([]), g_array ([, 0, bar]) example 8: |? -G_assign (T, g_array_auto (2, g_array_auto (2, null), g_read (T (2, 3), u), g_read (t, x ). U = nullx = g_array ([g_array ([null, null]), g_array ([null, null]), g_array ([null, null]), g_array ([null, null])

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.