# C[omp]ute

Welcome to my blog, which was once a mailing list of the same name and is still generated by mail. Please reply via the "comment" links.

Always interested in offers/projects/new ideas. Eclectic experience in fields like: numerical computing; Python web; Java enterprise; functional languages; GPGPU; SQL databases; etc. Based in Santiago, Chile; telecommute worldwide. CV; email.

© 2006-2015 Andrew Cooke (site) / post authors (content).

## Prolog, LEPL, Phone Numbers

From: andrew cooke <andrew@...>

Date: Wed, 10 Mar 2010 09:16:34 -0300

Here's a neat example that will be going into LEPL 4's documentation.  It
shows how you can use LEPL's backtracking a s search.  It also shows how the
new function wrappers allow you to write coroutines without having to know
what a coroutine is.

In case it's not clear, the "matcher" (Digit) matches a single character in
the phone number and gives, as a result, all possible ways that number could
be written (ie the original character plus any letters that might be used

Then we check that "1-800-painter" is in the results for '1-800-7246837' - see

from lepl import *

@sequence_matcher
def Digit(support, stream):
digits = {'1': '',     '2': 'abc',  '3': 'def',
'4': 'ghi',  '5': 'jkl',  '6': 'mno',
'7': 'pqrs', '8': 'tuv',  '9': 'wxyz',
'0': ''}
if stream:
digit, tail = stream[0], stream[1:]
if digit in digits:
for letter in digits[digit]:
yield ([letter], tail)
yield ([digit], tail)

results = Digit()[13, ...].match('1-800-7246837')
words = map(lambda result: result[0][0], results)
assert '1-800-painter' in words

The "map" near the end extracts the final results - that probably means that
the matcher API should be improved somehow.

Andrew