When would I require a Macro instead of a function?

I am new to Clojure, I am new to Macros and I have no prior background in Lisp.
I went on to create my own switch case like form and ended up with this:

(defmacro switch-case [v cases default] (if (cases v) (cases v) default )) 

and then tried making a function and ended up with this:

(defn fn-switch-case [v cases default] (if (cases v) (cases v) default ))

Both

(switch-case 5 {6 "six" 7 "seven"} "not there")

and

(fn-switch-case 5 {6 "six" 7 "seven"} "not there")

Work fine.

What could be the scenario where I would need a macro and a function wont work ?
Is there a drawback in my function or macro implementations ?

2

One benefit of macros is that they don’t follow the same evaluation rules as functions, since they’re just performing transformations on the code.

An example of a construct you couldn’t create just by using functions is Clojure’s threading macro.

It lets you insert an expression as the first argument in a sequence of forms:

(-> 5
  (+ 3)
  (* 4))

is equivalent to

(* (+ 5 3) 4)

You wouldn’t be able to create this kind of construct using only functions, since before the -> expression was evaluated, the inner (+ 3) and (* 4) would be, meaning -> would receive

(-> 5
    3
    4)

and it needs to see the actual functions being used in order to work.

In your example, consider if the result for some case were to have side-effects or call some other function. A function version would not be able to prevent that code from being run in a situation where that result is not picked, whereas a macro could prevent that code from being evaluated unless it is the option taken.

1

One fun aspect of macros are that they give you the possibility of expanding the syntax of your lisp and adding new syntactic features to it, and this happens only because arguments passed to a macro will get evaluated only at runtime, and not at the time of compiling your macro. As an example the first/last thread macros in clojure -> ->> don’t evaluate their expression-arguments, otherwise these evaluated results couldn’t accept anything more (say (+ 3) => 3 and 3 is not a function which could accept your first main argument in something like (-> 4 (+ 3))).

Now if i like this syntax and i want to add it to my Common Lisp implementation (which doesn’t have it) i can add it up by defining a macro myself. Something like this:

;;; in SBCL

;;; first thread:
(defmacro -> (x &rest more)
  (loop for m in more
     for n = `(,(first m) ,x ,@(rest m)) then `(,(first m) ,n ,@(rest m))
     finally (return n)))

;;; last thread:
(defmacro ->> (x &rest more)
  (loop for m in more
     for n = `(,(first m) ,@(rest m) ,x) then `(,(first m) ,@(rest m) ,n)
     finally (return n)))

Now i would be able to use them in Common Lisp the same way as in Clojure:

(-> #'+
    (mapcar '(2 3 4) '(1 2 3))) ;; => (3 5 7)

(->> #'>
 (sort '(9 8 3 5 7 2 4))) ;; => (9 8 7 5 4 3 2)

Also maybe you want to have a new syntax for clojure’s range function with your own keywords for your syntax, something like:

(from 0 to 10) ;=> (0 1 2 3 4 5 6 7 8 9)

(from 0 to 10 by 0.5) ;;=> (0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5)

can be defined like this macro:

(defmacro from
  "Just another range!"
  [x y z & more]
  `(when (= '~y '~'to)
     (if '~more (range ~x ~z (second '~more))
         (range ~x ~z))))

or add something similar to Common Lisp (it is only for example since all these can already be done in the languages of course!):

(defmacro from (x y z &rest r)
  `(when (eql ',y 'to)
     (if (and ',r (eql (first ',r) 'by))
     (loop for i from ,x to ,z by (second ',r) collect i)
     (loop for i from ,x to ,z collect i))))

(from 0 to 10) ;=> (0 1 2 3 4 5 6 7 8 9 10)
(from 0 to 5 by 1/2) ;=> (0 1/2 1 3/2 2 5/2 3 7/2 4 9/2 5)

I’m not exactly sure what you want your switch-case function to do. What should be the value of:

(def x 5)
(switch-case x {5 :here}
             :not-there)

It might be useful if you viewed the source of the related core functions/macros by using the repl

(clojure.repl/source case)

But in general a reason why you might want a macro as opposed to a function is that using the core case macro

(case 6
  5 (print "hello")
  "not there")

will not print hello.
However, if case were defined as a function, it print “hello” to the screen and the whole expression would evaluate to "not there". This is because functions evaluate their arguments before every calling the function.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật