1

i want to read 2 values from user input using lisp. I want to store those in variables so i can use them in my program.

So far i have the current code:

(defun le-posicao()
  (let ((n_anel) 
        (pos_anel))
    (princ "?")
    (setf n_anel (read))
    (setf pos_anel (read))
    (when (and (integerp (n_anel)) (integerp (pos_anel)))
          n_anel pos_anel)))

I'm creating the local variables with a let function and then i want to store the values i read from input in them.

My problem is, how can i read two values from input and store the first in one variable and the second in the other? The values are supposed to come in the following format:

? value1 value2

Can someone help me?

1 Answer 1

1

You're trying to use Lisp imperatively. While this is possible, it may be slightly painful.

To make your code more functional, instead of creating variables and then mutating them, you should create names and bind values to them.

To clarify, let is a special form, not a function; they are used to create lexically scoped bindings of names to values.

I believe what you really want to do is:

(let ((n_anel (read))
      (pos_anel (read)))
  (... code using but not modifying n_anel and pos_anel ...))

This code avoids setf.


Notes:

Not sure about the parentheses in Common-Lisp's let -- I've been using Clojure recently!

Also not sure whether you're using read correctly.

Sign up to request clarification or add additional context in comments.

3 Comments

Using this method i'm only being able to read the last value from the input. I want to store the first value in the n_anel variable and the second one on pos_anel.
The problem was i was not returning a list with both values, so i could only see the last one. It's solved now, thanks a lot.
You should have to use let* instead of let. let performs the bindings in parallel and let* does them sequentially.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.