Pattern matching binaries.

29 views
Skip to first unread message

Mason Staugler

unread,
Feb 27, 2015, 6:52:12 AM2/27/15
to lisp-flavo...@googlegroups.com
In this post there's an example of pattern matching with binaries, but this is what I'm getting now.

(defun foo
 
([(binary 40 (binary rest))]
    rest
)
 
([_]
   
'unmatched))
> (foo #B("(hello"))
exception error: #(undefined_bittype rest)

Ultimately, I'm looking for the binary equivalent of:

> (defun bar
   
([(list* #\f #\o #\o rest)]
      rest
)
   
([_]
     
'unmatched))
bar
> (bar "foobar")
"bar"


Robert Virding

unread,
Feb 27, 2015, 10:40:00 AM2/27/15
to lisp-flavo...@googlegroups.com
Hi Mason,

That is my fault as the pattern is wrong. A binary constructor/patten consists of a sequence of segments where each segment has the form (val <specs> ...). A spec is either type (integer,binary,bitstring,float), (size n), big|little-endian, signed|unsigned. The default type is integer. So the pattern should have read (binary 40 (rest binary)) and the function


(defun foo
 
([(binary 40 (rest binary))] rest)
 
([_] 'unmatched))


Sorry about that. A description binaries is at about 2/3 of the file doc/user_guide.txt or in http://docs.lfe.io/user-guide/diving/3.html .

Robert

Mason Staugler

unread,
Feb 27, 2015, 10:51:42 AM2/27/15
to lisp-flavo...@googlegroups.com
That makes sense. Thanks, Robert!
Reply all
Reply to author
Forward
0 new messages