An edge-detector in Lisp

An edge detector detects transitions between two symbols in the input sequence, say 0 and 1.

It outputs 0 as long as the most recent input symbol is the same as the previous one.

However, when the most recent one differs from the previous one, it outputs a 1.

By convention, the edge detector always outputs 0 after reading the very first symbol.

(let ((previous))
  (defun edge-detector (input)
    (let ((output 0))
      (when (and (not (null previous)) 
                 (/= previous input)) 
        (setf output 1))
      (setf previous input)
      (return-from edge-detector output))))
Published in: on 25/03/2013 at 00:12  Leave a Comment  

The URI to TrackBack this entry is:

RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: