2017-04-18 05:31:40

Hi,

I heard about this awesome language from a friend in college. And it has been a joy to use, albeit a bit weird to get the syntax. Basically, instead of making a function call like foo(bar, baz, qux), you put the parenthesis out front and the function name first, with its arguments separated by white space. Here is how the aforementioned function call would look in real Lisp code:
(foo bar baz qux)
This means take foo, and apply it to the arguments bar baz and qux. Now how about a real example?
Suppose that we want to implement the classic n! problem in Lisp. In the languages we know and love, this would look somewhat like this:
def factorial(n):
    """ Return the factorial of n."""
    if n <= 0: return 1
    else: return n*factorial(n-1)
Now, let's take this definition and make it Lispy!
(defun factorial (n)
  "Return the factorial of n"
  (if (<= n 0)
  1
  (* n (factorial (- n 1)))))
Now, I guess I've got some 'splaining to do.
We'll take this chunk by chunk:
1. The defun macro basically tells your Lisp interpreter "Okay, interpreter, the next thing is going to be called a function called factorial. It will take as an argument a number n."
2. The "Returns the factorial of n" bit is a docstring, akin to what is in Python.
3. Now, we get to the bit of code that does the heavy lifting. This (if (<= n 0) part is a call to the if function. Its first argument is a call to the <= function, to see if n is <= 0. Note, however, that I did not close off the if list; if I did this, the forms that follow would not evaluate.
4. If the first condition, n <= 0 yields true, we simply return the atom 1 (atoms are essentially basic values in Lisp). This is the base case for our recursive call; 0! = 1 and we need to stop there, else we will recursively loop to infinity.
5. Now, we get to the last bit of code.
a. We'll start out by starting a call to the * function. Its first argument is the value we passed in originally, n.
b. Now, we make a call to the factorial function.
c. Then, we call the - function of n-1. Since n! is defined as n*(n-1)!, this is our recursive call.
6. And now, we close off the lists of function calls; in Lisp, it seems to be customary to put all the closing parenthesis on the last line.
And there you have it, an example of Lisp code. Not exactly revolutionary, but I think it might start an interesting discussion. To test the code and prove it, you'll need, naturally, a Lisp interpreter. As I am on Linux, I use SBCL, which I belive has been ported to Windows and Mac.
Happy hacking!

2017-04-18 18:33:57

Lisp is too complicated for me, but It's certainly interesting to study it.
For anyone who wants to learn lisp (or, more accurately, the Scheme derivative), you can check out Andrew Plotkin's Lists and Lists, which is a Scheme interpreter written in Inform.

“Can we be casual in the work of God — casual when the house is on fire, and people are in danger of being burned?” — Duncan Campbell
“There are four things that we ought to do with the Word of God – admit it as the Word of God, commit it to our hearts and minds, submit to it, and transmit it to the world.” — William Wilberforce

2017-04-19 15:08:41

This is exactly why I dislike LISP and it's derrivitives. The syntax is entirely comprised of lists, which makes it too confusing for most people to grasp. Not everyone can keep track of a thousand parentheses... at least languages like Python have some common-language elements to them.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2017-04-19 16:51:37

I have to agree with you Ethin. It seems just too complicated.

“Can we be casual in the work of God — casual when the house is on fire, and people are in danger of being burned?” — Duncan Campbell
“There are four things that we ought to do with the Word of God – admit it as the Word of God, commit it to our hearts and minds, submit to it, and transmit it to the world.” — William Wilberforce

2017-04-20 08:15:51

I used to think the same thing. It is much easier when you use an interface like SLIME for Emacs; it will keep track of your parenthesis, give you documentation for function calls, and a whole host of other things.

2017-04-21 20:10:49

@king gamer222, that doesn't matter. We shouldn't have to rely on other tools to keep track of something we should be able to keep track of ourselves. LISP is simply no longer a language that very many use -- C++ is far, far more preferable.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github