Sinisterly
[Lisp] Functions vs. Macros - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: Coding (https://sinister.li/Forum-Coding--71)
+--- Thread: [Lisp] Functions vs. Macros (/Thread-Lisp-Functions-vs-Macros)



[Lisp] Functions vs. Macros - Inori - 08-31-2015

There are 2 somewhat equivalent "def" functions in Lisp: defun and defmacro.

The difference between these two is macros are used for defining syntax extension to Common Lisp or Domain Specific Languages (DSLs). These languages are embedded right within your existing Lisp code.
As a more concrete example, Python has list comprehensions built-in the language. This gives a simple syntax for a common case [1] gives the numbers from 0 to 9 that are divisible by two. Back in Python 1.5 there was no such syntax, and that would have expressed as:
Code:
divisibleByTwo = [] for x in range( 10 ): if x % 2 == 0: divisibleByTwo.append( x )

[1]
Code:
divisibleByTwo = [x for x in range(10) if x % 2 == 0]

The above examples are functionally equivalent, yes?

In Lisp you could write the following:
Code:
;; the first two functions are a clone of Python's "range" (defun range-helper (x) (if (= x 0) (list x) (cons x (range-helper (- x 1))))) (defun range (x) (reverse (range-helper (- x 1)))) ;;equal to divisibleByTwo = [] (setq divisibleByTwo nil) (loop for x in (range 10) if (= (mod x 2) 0) do (setq divisibleByTwo (append divisibleByTwo (list x))))

Instead of doing all that messy garbage, we can define a macro called lcomp (short for list comprehension). It's syntax will be exactly like the python that we used in the example.
Code:
(defmacro lcomp (expression for var in list conditional conditional-test) (let ((result (gensym))) `(let ((,result nil)) (loop for ,var in ,list ,conditional ,conditional-test do (setq ,result (append ,result (list ,expression)))) ,result))) ;; we need a variable for our macro to take in this case (setq l '(0 1 2 3 4 5 6 7 8 9)) ;; ' for literal definition (write (lcomp x for x in l if (= (mod x 2) 0)))

Both programs give the same output.
Lisp:
Code:
(0 2 4 6 8)
Python:
Code:
[0, 2, 4, 6, 8]

Huge props to gte525u and Slomojo from StackOverflow for reference on this topic. If you're confused as hell after reading this, go read this.


RE: [Lisp] Functions vs. Macros - dotcppfile - 09-01-2015

Ah that's nice even though people around here don't know the fuck is going on but it's still a nice share, good work mate.


RE: [Lisp] Functions vs. Macros - Inori - 09-01-2015

(09-01-2015, 12:05 AM)dotcppfile Wrote: Ah that's nice even though people around here don't know the fuck is going on but it's still a nice share, good work mate.

Anyone who knows basic Python (and read my previous Lisp thread) should have no trouble with this.


RE: [Lisp] Functions vs. Macros - dotcppfile - 09-01-2015

(09-01-2015, 12:10 AM)Chitoge Wrote: Anyone who knows basic Python (and read my previous Lisp thread) should have no trouble with this.

Nah, they don't know anything about cons, let, setq, do etc so they won't get it, that's a fact mate.


RE: [Lisp] Functions vs. Macros - Inori - 09-01-2015

(09-01-2015, 12:18 AM)dotcppfile Wrote: Nah, they don't know anything about cons, let, setq, do etc so they won't get it, that's a fact mate.

It's not entirely about the content of code, the thread itself is comparing Functions to Macros, and anyone can blatantly see that Macros are better suited in this case.


RE: [Lisp] Functions vs. Macros - dotcppfile - 09-01-2015

(09-01-2015, 12:44 AM)Chitoge Wrote: It's not entirely about the content of code, the thread itself is comparing Functions to Macros, and anyone can blatantly see that Macros are better suited in this case.

We can argue all day mate but this shit is clear, this thread isnt just about code but code is needed to prove your point and you used Lisp for it...

I'll stop replying because whats going on here is obvious.


RE: [Lisp] Functions vs. Macros - phyrrus9 - 09-01-2015

I would have to agree...you chose the worst two languages you could to explain what a macro and function are....

The way I define them is macros are like a massive find replace, while functions are real code.