I am not experienced in LISP and list processing, but I have a set of C++ STL vectors (or strings) on which I need to perform the following operations:
IdenticalHead (v1, v2): Return the biggest sequence that both v1 and v2 start with it.
IdenticalTail (v1, v2): Return the biggest sequence that both v1 and v2 end with it.
IdenticalTailHead (v1, v2): Return the biggest sequence that v1 ends with it and v2 starts with it.
For example:
if v1 = (a,b,c,e,f), v2 = (a,b,d,e,f), then:
IdenticalHead (v1, v2) = (a,b)
IdenticalTail (v1, v2) = (e,f)
if v1 = (a,b,c), v2 = (b,c,g), then:
IdenticalTailHead (v1, v2) = (b,c)
My question is that are these standard operations in LISP or any other language? Do they have standard names like CDR and CAR?
No, there are no standard operators for these. But
identical-headis not hard to write:I don’t know of an easier way to write
identical-tailexcept reversing the input lists, callingidentical-head, and then reversing the result, since lists only allow forward traversal, not backward.Here’s how I’d write
identical-tail-head:It’s not a very efficient way to do it, since it’s O(n²), but (again) given that lists are forward-traversal-only, I haven’t come up with a better way.