[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Multiple strings pattern matching ... How?

Frank GOENNINGER

5/6/2016 5:10:00 PM

Hi all,

I am trying to come up with some function that takes a definition of
patterns and a function and, when an input string matches, calls the
given function - like so:

(defun set-frequency (arg)
... do some clever stuff here )

(defun get-frequency ()
... read frequency from radio and return it ... )

(defparameter *pattern-defs* (list (cons (list "F *;") 'set-frequency)
(cons (list "f;" 'get-frequency))))

(defun match-and-dispatch (string)
(match-against-all-pattern-defs-and-call-function-on-match
*pattern-defs*))

Ideally, this would take advantage of multicore CPUs as I expect to have
lots of pattern defs like "X *;" ( a character X followed by a space
followed by an undefined number of digits followed by a semicolon -
that's the basic scheme of the patterns).

I have tried to find some library or code but only came up with
cl-unification ... Is this the way to go? How would I make 'unify accept
the list of patterns?

Thanks for providing some hints!

Regards from sunny South-West Germany

Frank

2 Answers

William James

5/6/2016 6:50:00 PM

0

Frank DG1SBG wrote:

> Hi all,
>
> I am trying to come up with some function that takes a definition of
> patterns and a function and, when an input string matches, calls the
> given function - like so:
>
> (defun set-frequency (arg)
> ... do some clever stuff here )
>
> (defun get-frequency ()
> ... read frequency from radio and return it ... )
>
> (defparameter *pattern-defs* (list (cons (list "F *;") 'set-frequency)
> (cons (list "f;" 'get-frequency))))
>
> (defun match-and-dispatch (string)
> (match-against-all-pattern-defs-and-call-function-on-match
> *pattern-defs*))
>
> Ideally, this would take advantage of multicore CPUs as I expect to have
> lots of pattern defs like "X *;" ( a character X followed by a space
> followed by an undefined number of digits followed by a semicolon -
> that's the basic scheme of the patterns).
>
> I have tried to find some library or code but only came up with
> cl-unification ... Is this the way to go? How would I make 'unify accept
> the list of patterns?

OCaml:

#load "str.cma";;
open Str;;

let foo str = Printf.printf "foo called with %s\n" str ;;
let bar str = Printf.printf "bar called with %s\n" str ;;

let pats_with_funcs =
[regexp "^F [0-9]+;$", foo;
regexp "^f;$", bar] ;;

let match_and_dispatch str =
let (_,func) =
List.find
(fun (pat,func) -> string_match pat str 0)
pats_with_funcs
in func str ;;

match_and_dispatch "F 8;";;
===>
foo called with F 8;

match_and_dispatch "f;";;
===>
bar called with f;

--
[A]n unholy alliance of leftists, capitalists, and Zionist supremacists has
schemed to promote immigration and miscegenation with the deliberate aim of
breeding us out of existence in our own homelands.... [T]he real aim stays the
same: the biggest genocide in human history.... --- Nick Griffin
(https://www.youtube.com/watch?v=K...)

Carlos

5/6/2016 10:39:00 PM

0

[Frank DG1SBG <dg1sbg@googlemail.com>, 2016-05-06 19:09]
> Hi all,
>
> I am trying to come up with some function that takes a definition of
> patterns and a function and, when an input string matches, calls the
> given function - like so:
>
> (defun set-frequency (arg)
> ... do some clever stuff here )
>
> (defun get-frequency ()
> ... read frequency from radio and return it ... )
>
> (defparameter *pattern-defs* (list (cons (list "F *;") 'set-frequency)
> (cons (list "f;" 'get-frequency))))
>
> (defun match-and-dispatch (string)
> (match-against-all-pattern-defs-and-call-function-on-match
> *pattern-defs*))
>
> Ideally, this would take advantage of multicore CPUs as I expect to
> have lots of pattern defs like "X *;" ( a character X followed by a
> space followed by an undefined number of digits followed by a
> semicolon - that's the basic scheme of the patterns).
>
> I have tried to find some library or code but only came up with
> cl-unification ... Is this the way to go? How would I make 'unify
> accept the list of patterns?
>
> Thanks for providing some hints!

Maybe lparallel[1]? You can use #'pmapc to traverse the list, and match
with some function from cl-ppcre or your implementation's posix package.

[1] https://lpar...

--