jakedoeyo
jakedoeyo

Reputation: 43

DNA Matching LISP

I am currently trying to use LISP to make a function that looks at DNA pairs and returns either T or NIL depending on if they match or not

Logically I know you need to go through the different possible cases recursively, but am just not quite sure how to do this in LISP. I would appreciate any help you can provide.

I know that I now need to go through and compare A to T, and C to G, but I have no clue how to do this.

EDIT: Test cases to show what I mean more clearly

(dnamatching '(a t c g) '(a c g g))
==> NIL
(dnamatching '(a a t c g) '(t t a g c))
==> T

Upvotes: 0

Views: 100

Answers (1)

sds
sds

Reputation: 60014

Here is a basic version

(defun dnamatch-1 (x y)
  (ecase x
    ((a) (eq y 't))
    ((t) (eq y 'a))
    ((c) (eq y 'g))
    ((g) (eq y 'c))))

(defun dnamatch-list (a b)
  (every #'dnamatch-1 a b))

(dnamatch-list '(a t c g) '(a c g g))
==> NIL
(dnamatch-list '(a a t c g) '(t t a g c))
==> T

If you prefer, here is an iterative version instead:

(defun dnamatch-list (a b)
  (loop for x in a and y in b
    always (dnamatch-1 x y)))

Documentation:

Notes

nucleobase representation

You are using the standard constant t as name for a nucleobase, which is probably not the best approach. You might want to use characters (#\t &c) or longer symbols (thymine &c) instead. If you decide to use characters, you might want to use strings instead of lists.

Error checking

Note that my dnamatch-1 checks that x contains only valid symbols but does not check y.

length checking

Both versions of dnamatch-list only compare the shortest common part of its arguments, you probably would want to do something like

(defun dnamatch-list (a b)
  (and (= (length a) (length b))
       (every #'dnamatch-1 a b)))

Upvotes: 1

Related Questions