0

I'm trying to write a program that takes user input for a single integer, and then a list of integers, then outputs the indices where the values match in an array.

Ex:

Enter integer X: 5

Enter list of Integers i1 i2 i3 ...): 1 2 5 5 3

As it is I'm passing an array of the inputted list values to the function array-compare, and trying to push the values into a globally defined array 'resultArray'.

(format t "Enter integer X: ") 
(defvar X (read))
(format t "Enter list of Integers i1 i2 i3 ...): ")
(defvar input (read-line))
(defvar L (string-trim '(#\Space #\Tab #\Newline) input))



(defun read-array (L)
 (with-input-from-string (in L)
  (loop for x = (read in nil nil) while x collect x))
)


(defun list-to-array (list)
  (make-array (list (length list))
              :initial-contents list))

(setf numList (read-array L))
(setf numArray (list-to-array numList))
(setf resultArray (make-array (length numArray) :fill-pointer 0))

(defun array-compare (arr)
    (loop for y in arr
       do((if (= y X)
                (vector-push y resultArray)))))


(format t "~a" (array-compare numArray))

Expected output: (3,4)

I keep getting the error

Badly formed lambda: (IF (= Y X) (VECTOR-PUSH Y RESULTARRAY))

I know it has something to do with the declarative nature of the language, and it's probably something obvious I'm overlooking. Also I realize that as is the code will only print the elements, not their indices, but I'm trying to get the elements as a proof of concept first. I'm fairly proficient in object-oriented languages, but having a tough time with LISP, any and all help would be greatly appreciated.

sds
  • 58,617
  • 29
  • 161
  • 278
bruce_M
  • 1
  • 1
  • 3
  • `((if` has how many parentheses too much... – Rainer Joswig Dec 05 '17 at 08:01
  • This kind of error is common among beginners. In Lisp parentheses are significant. On the other hand this error message is not particularly helpful. I guess the OOP analog would be `foo()()` when you mean `foo()` – Dan Robertson Dec 05 '17 at 10:17
  • [Lisp parentheses are _meaningful_](https://stackoverflow.com/a/46224551/850781) – sds Dec 05 '17 at 14:15

0 Answers0