0

Using acl2, I am trying to create a function, "ins" that recursively appends the first argument (a list) to the front of each element in the second argument (another list), where

(ins (something) ( (a b c) (d e f) ...))

returns

( (something a b c) (something d e f) ...)

so a call to the function as such

(ins '((one thing)) '( ((this is)) ((something else)) ))

will give us

'( ((one thing) (this is)) ((one thing) (something else)) ))

I've come up with non-recursive function that only works on arg2 lists containing a single element, checking if empty.

(defun ins(arg1 arg2)

  (if (equal arg2 nil) '() (list(append  arg1 (first arg2))))

)

When I try to come up with something recursive, so that it appends the first argument to all elements in the list of the second argument, the best I can do is

(defun ins (arg1 arg2)

  (cond

   ((equal arg2 nil) '())

   ((not(equal arg2 nil)) (ins (list(append  arg1 (first arg2))) (first(rest arg2)))

   )))

But I always get a nil no matter what, and I can't seem to figure out why. As such, I don't even know whether my recursive call is even correct. I just have a difficult time tracing non-trivial recursion.

imgoingmad
  • 95
  • 3
  • 10

1 Answers1

2

Something like this?

(defun ins (arg1 arg2)
  (if arg2
      (cons (append arg1 (car arg2)) 
            (ins arg1 (cdr arg2)))
      '()))
uselpa
  • 18,732
  • 2
  • 34
  • 52
  • "when" isn't in the common-lisp package though. I will look it through regardless, thanks. I'm sure there's another way of writing it. – imgoingmad Dec 01 '15 at 21:42
  • You can just use `if`instead in this case. – uselpa Dec 01 '15 at 21:43
  • I don't believe if and when can be used interchangeably. While I don't know how "when" here works, if requires 3 arguments, where it's used as such- (if test then else). There are only 2 arguments used here. – imgoingmad Dec 01 '15 at 21:48
  • Yes, I did. Told me I needed 3 arguments. – imgoingmad Dec 01 '15 at 21:49
  • It's working! Just add a nil at the end as the last argument for if. Thanks so much! (or yes, in your case, an empty list) – imgoingmad Dec 01 '15 at 21:52
  • You're welcome. Sorry for the hassle, I don't have ACL2 installed, just plain Common Lisp. – uselpa Dec 01 '15 at 21:54