Ask Your Question
1

What is the method to generate a return statement in Common Lisp?

asked 2022-05-04 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-02-21 22:00:00 +0000

plato gravatar image

To generate a return statement in Common Lisp, you can use the built-in function return-from.

The syntax for return-from is as follows:

return-from tagname value

Here, tagname is an identifier that represents the block of code to which the return statement applies, and value is the optional value to be returned.

For example, suppose you have a function square-root that computes the square root of a number. You could use return-from to exit the function early and return a value if an error condition occurs:

(defun square-root (x)
  (if (< x 0)
      (return-from square-root nil)
      (sqrt x)))

In this example, if x is negative, the if statement returns nil by calling return-from with the identifier square-root.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-05-04 11:00:00 +0000

Seen: 3 times

Last updated: Feb 21 '22