Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I pass any number of arguments to a function?

Here is what I’ve tried, but you’re hoping that when someone uses the function, they pass an array.

(defn make-sandwich
   [items]
   
   (print "I'll make you a great sandwich:")
   (each item items (print "Adding " item  " to your sandwich." ))
   (print "Your sandwich is ready!"))

(make-sandwich @["Chicken" "Cheddar Cheese" "Lettuce" "Salad Dressing"])

but I’m not sure that’s correct.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You don’t need to pass an array, you can use & array_name to accept any number of arguments:

(defn make-sandwich
   [& items]
   
   (print "I'll make you a great sandwich:")
   (each item items (print "Adding " item  " to your sandwich." ))
   (print "Your sandwich is ready!"))

(make-sandwich "Chicken" "Cheddar Cheese" "Lettuce" "Salad Dressing")

All the items passed will be stored in a tuple.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading