(function [args] more args)
Hi again. This is another syntax that I'm struggling with: (function [args] more args) Or for example: (subvec [1 2 3 4 5] 1 3) Please note I'm not referring specifically to the subvec function, but simply using it as an example, as I've seen this syntax with many other functions, but it escapes my mind now to provide more examples. I don't like it, and here's what I don't like about it. It leaves me with a bad taste that where the arguments are generally meant to be passed to the function in a vector of arguments, some are sometimes passed outside the vector. It feels inconsistent and ad hoc. What am I missing out on? are the arguments contained within a vector only when defining functions? such as: (defn name [args] body) Thanks. -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: (function [args] more args)
So let me see if I can help out with this. In classic lisp, when you define a function it would take this syntax: (defn name (arg1 arg2) body) The only problem with this approach is that sometimes it is hard to figure out what is part of the body and what is the argument lists. Clojure solves this by just mandating that argument lists are vectors...this is almost purely syntactic sugar. The idea is that using [] makes it stand out from the other forms that use (). Now in the example you gave for subvec...we would write this in classic lisp like this: (subvec (vector 1 2 3 4 5) 1 3) So we're creating a vector then running subvec on it. However, this is a bit more verbose that what people from say Ruby and Python are used to. It's just simpler to allow people to write [1 2 3 4 5] instead of (vector 1 2 3 4 5) So in the case of using [] as a function argument, it's considered a vector constructor. In the case of being used in a defn, it's considered syntactic sugar. Yes it's a tad confusing, but it makes sense once you work it out. I hope this helps. Timothy -- “One of the main causes of the fall of the Roman Empire was that–lacking zero–they had no way to indicate successful termination of their C programs.” (Robert Firth) -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Aw: (function [args] more args)
Hi, in your example the vector *is* the argument. You could just as well write (let [x [1 2 3 4 5]] (subvec x 1 3)). On function definition the arguments are given in a vector, yes. I'm not sure I understand your concern completely. Sincerely Meikel -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: (function [args] more args)
The enclosed vector (or list, map, set, etc) is considered as arg1 because it is one entity. Take a look at the source for subvec: = (clojure.repl/source subvec) (defn subvec Returns a persistent vector of the items in vector from start (inclusive) to end (exclusive). If end is not supplied, defaults to (count vector). This operation is O(1) and very fast, as the resulting vector shares structure with the original and no trimming is done. {:added 1.0} ([v start] (subvec v start (count v))) ([v start end] (. clojure.lang.RT (subvec v start end nil Notice that it takes 2 or 3 arguments. Your example, (subvec [1 2 3 4 5] 1 3), correspond with the 3 args method. You can verify this by: = (count '([1 2 3 4 5] 1 3)) 3 So the correct definition for something like subvec is (function [vector_arg1 arg2 arg3] body). For more info on how to play with the function arguments, look into 'clojure destructuring'. On Jun 15, 11:31 am, James Keats james.w.ke...@gmail.com wrote: Hi again. This is another syntax that I'm struggling with: (function [args] more args) Or for example: (subvec [1 2 3 4 5] 1 3) Please note I'm not referring specifically to the subvec function, but simply using it as an example, as I've seen this syntax with many other functions, but it escapes my mind now to provide more examples. I don't like it, and here's what I don't like about it. It leaves me with a bad taste that where the arguments are generally meant to be passed to the function in a vector of arguments, some are sometimes passed outside the vector. It feels inconsistent and ad hoc. What am I missing out on? are the arguments contained within a vector only when defining functions? such as: (defn name [args] body) Thanks. -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: (function [args] more args)
Hi, I admit that subvec is not a good example as it does indeed take a vector as a first argument, perhaps i'll find better example or perhaps I might've just been confused. I learnt lisp and scheme many years ago, abandoned them for languages with better libraries, and I'm perhaps thrown off by the [] of clojure instead of the () throughout of lisp. Thanks. On Jun 15, 4:54 pm, Meikel Brandmeyer m...@kotka.de wrote: Hi, in your example the vector *is* the argument. You could just as well write (let [x [1 2 3 4 5]] (subvec x 1 3)). On function definition the arguments are given in a vector, yes. I'm not sure I understand your concern completely. Sincerely Meikel -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en