Page 192 - DCAP310_INTRODUCTION_TO_ARTIFICIAL_INTELLIGENCE_AND_EXPERT_SYSTEMS
P. 192

Introduction to Artificial Intelligence & Expert Systems




                    Notes          definition still applies in the very specific case of the input being 0, while for any other argument
                                   the function returns n  * f (n-1) with n being the argument. The wildcard pattern (often
                                   written as _) is also simple: like a variable name, it matches any value, but does not bind the
                                   value to any name.

                                   Self Assessment

                                   State whether the following statements are true or false:
                                   1.  Tree patterns are used in all programming languages as a general tool to process data
                                       based on its structure.
                                   2.  Pattern matching sometimes include support for guards.
                                   3.  Sequence patterns are often described using regular expressions and matched using
                                       techniques.

                                   10.2 Measure for Matching

                                   More complex patterns can be built from the primitive ones of the previous section, usually in
                                   the same way as values are built by combining other values. The difference then is that with
                                   variable and wildcard parts, a pattern doesn’t build into a single value, but matches a group of
                                   values that are the combination of the concrete elements and the elements that are allowed to
                                   vary within the structure of the pattern.
                                   A tree pattern describes a part of a tree by starting with a node and specifying some branches and
                                   nodes and leaving some unspecified with a variable or wildcard pattern. It may help to think of
                                   the abstract syntax tree of a programming language and algebraic data types.
                                   In Haskell, the following line defines an algebraic data type  Color that has a single data
                                   constructor ColorConstructor that wraps an integer and a string.
                                     data Color = ColorConstructor Integer String
                                   The constructor is a node in a tree and the integer and string are leaves in branches.

                                   When we want to write functions to make Color an abstract data type, we wish to write functions
                                   to interface with the data type, and thus we want to extract some data from the data type, for
                                   example, just the string or just the integer part of Color.

                                   If we pass a variable that is of type Color, how can we get the data out of this variable? For
                                   example, for a function to get the integer part of Color, we can use a simple tree pattern and
                                   write:
                                     integerPart (ColorConstructor theInteger _) = theInteger
                                   As well:
                                     stringPart (ColorConstructor _ theString) = theString
                                   The creations of these functions can be automated by Haskell’s data record syntax.

                                   Filtering Data with Patterns

                                   Pattern matching can be used to filter data of a certain structure. For instance, in Haskell a list
                                   comprehension could be used for this kind of filtering:
                                     [A x|A x <- [A 1, B 1, A 2, B 2]]
                                   evaluates to
                                     [A 1, A 2]



          186                               LOVELY PROFESSIONAL UNIVERSITY
   187   188   189   190   191   192   193   194   195   196   197