1

I have the following list:

(1 (2))

And I want to subsitute (2) with (2 (3)) in order to obtain:

(1 (2 (3)))

The use of subst as followed does not return the wished result:

(subst '(2 (3)) '(2) '(1 (2)))

Is there a simple way to perform the substitution?

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Xaving
  • 329
  • 1
  • 11
  • possible duplicate of [Test if array is inside a list in lisp](http://stackoverflow.com/questions/19287777/test-if-array-is-inside-a-list-in-lisp) – sds Aug 19 '14 at 16:01

1 Answers1

6

By default subst uses eql, you must specify :test argument to use #'equal, to get what you want.

CL-USER> (subst '(2 (3)) '(2)  '(1 (2)) :test #'equal)
(1 (2 (3)))

As you see, two directly specified lists aren't EQL, but they are EQUAL:

CL-USER> (eql '(2) '(2)) 
NIL
CL-USER> (equal '(2) '(2))
T

Read more about eq, eql, equal and eqaulp difference in lisp.

sheikh_anton
  • 3,422
  • 2
  • 15
  • 23
  • 2
    It's also worth noting that **eql** isn't just **subst**'s default. According to [17.2.1 Satisfying a Two-Argument Test](http://www.lispworks.com/documentation/HyperSpec/Body/17_ba.htm), "If neither a **:test** nor a **:test-not** argument is supplied, it is as if a **:test** argument of **#'eql** was supplied." That applies to the functions listed on that page (which includes **subst**). – Joshua Taylor Aug 19 '14 at 15:05