Thanks again. I do get it. I'm not saying that apply has effects other than 
the result it returns. No, it's not a side-effect; it doesn't change 
something in the environment in passing. What it changes is the expected 
result, if you expect that, as Rich Hickey is supposed to have written,
 (apply f [i]) can be written (f i)

This effect is, on the face of it, unpredictable: you just have to know 
that that is what apply does.  Users of clojure learn that pretty quickly. 
I've just learned it. Doc doesn't help.

user=> (doc apply)
>
> -------------------------
>
> clojure.core/apply
>
> ([f args] [f x args] [f x y args] [f x y z args] [f a b c d & args])
>
>   Applies fn f to the argument list formed by prepending intervening 
>> arguments to args.
>
> nil
>
>
>  

If you are looking, for some reason I can't imagine at the moment, for a 
function that acts just like a funcall, so that
(funcall f [i]) is exactly equivalent to (f [i]), I guess you are out of 
luck.

I don't know how other lisps deal with "linearising" argument lists? I'd be 
interested to hear.

On Sunday, 16 December 2012 10:21:36 UTC+10, puzzler wrote:
>
> On Sat, Dec 15, 2012 at 3:58 PM, Peter West <peter....@gmail.com<javascript:>
> > wrote:
>
>> But it can't, can it? In this context (apply f [i]) with respect to (f i)
>> * apply *has *side-effects*!
>>
>
> This doesn't really make any sense.  There are no side effects here.  I 
> think maybe you just don't understand what apply does.  Let me try to 
> explain it a different way.
>
> Some functions, like +, are designed to take 1, 2, 3, 4, 5, or any number 
> of arguments.  You can write:
> (+ 1 2)
> (+ 1 2 3)
> (+ 1 2 3 4)
> (+ 1 2 3 4 5)
> etc.
> The "contract" for + is that it takes any number of numbers as an input.
>
> One thing + can't do is add up the numbers in a list, at least not when 
> written in this way:
> (+ [1 2 3 4 5])
> This doesn't make any sense, because + is expecting numbers, and we passed 
> it a vector.
>
> It might seem somewhat ironic that + can't take a list of numbers.  
> Obviously, it knows how to add a series of numbers, it just expects them to 
> be one after each other, as arguments, not all bundled up in a list.
>
> If only we could delete those brackets around [1 2 3 4 5]...  If only we 
> had some sort of "adapter" that could take a list/vector of items, and pass 
> them in as multiple arguments to a function that was designed to take 
> multiple arguments...
>
> That is what apply does.
>
> (apply + [1 2 3 4 5]) turns into
> (+ 1 2 3 4 5)
>
> Similarly,
> (apply str [\a \b \c \d]) turns into
> (str \a \b \c \d)
>
>
>

-- 
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

Reply via email to