[Cs254f11] Using collection items as functions

Lee Spector lspector at hampshire.edu
Fri Sep 30 15:05:32 EDT 2011


PS: On why it didn't give an error message, at least that behavior is consistent with:

('+ 2 4)

Which returns 4. That quoted + is not the addition function; it's just a quoted symbol. If you want the addition function you have to evaluate the symbol, which is what happens with (+ 2 4).

 -Lee


On Sep 30, 2011, at 2:59 PM, Lee Spector wrote:

> 
> First, let's look at two versions that DO work:
> 
> ((rand-nth [+ - * /]) 2 4)
> 
> and
> 
> ((eval (rand-nth '(+ - * /))) 2 4)
> 
> See what's going on?
> 
> The expression (rand-nth '(+ - * /)) returns an unevaluated symbol like + or -, etc., but not the functions that have those symbols as their names.
> 
> When you type something like (+ 2 4) the + symbol gets evaluated to give you the addition function which is then called on 2 and 4. But your rand-nth expression returned an unevaluated symbol.
> 
> In my first version above I used a vector literal instead of a list literal, and you don't have to quote the vector literal. You have to quote a literal list because you don't want it to be treated as a function call. When you write a vector literal as I did, with no quote, all of the things inside of a vector literal WILL be evaluated. So the first thing in [+ - * /] will be the result of evaluating +, which is the addition function. And of course all of the other function names will be evaluated too.
> 
> In my second version I still had rand-nth returning an unevaluated symbol, but then I explicitly evaluated it with eval.
> 
> Come to think of it, here's a third version using something fancier, the "back-quote." If you use back-quote instead of a regular quote then you can use ~ to "un-quote" things within the quoted list:
> 
> ((rand-nth `(~+ ~- ~* ~/)) 2 4)
> 
> Probably the nicest one is the first one above, with the vector.
> 
> One thing that I haven't explained is why you weren't getting an error message from what you did, and why it was instead treating the expression as if it had an implicit "do" (evaluating everything and returning the last value)... and the reason I haven't explained it is that it's not clear to me either! But I hope I've made it clear why your expression didn't do what you wanted, and how you can change it to do the right thing.
> 
> -Lee
> 
> 
> On Sep 30, 2011, at 1:47 PM, Maxwell William Fair Levit wrote:
> 
>> 
>> 
>> Howdy,
>> 
>> So I'm trying to generate random expressions by listing said expressions and
>> picking one at random. I'm doing it like this:
>> 
>> (rand-nth '(+ - * /))
>> 
>> Problem is, when I try to use that as a function, it doesn't work so well:
>> 
>> ((rand-nth '(+ - * /)) 2 4)
>> 
>> 4
>> 
>> This just returns the last item in the above call.
>> 
>> I assume that the problem is that Clojure is not recognizing the randomly chosen
>> element as its functional counterpart, is that whats going on? And how do I fix
>> it?
>> 
>> -Max
>> _______________________________________________
>> Cs254f11 mailing list
>> Cs254f11 at lists.hampshire.edu
>> https://lists.hampshire.edu/mailman/listinfo/cs254f11
> 
> --
> Lee Spector, Professor of Computer Science
> Cognitive Science, Hampshire College
> 893 West Street, Amherst, MA 01002-3359
> lspector at hampshire.edu, http://hampshire.edu/lspector/
> Phone: 413-559-5352, Fax: 413-559-5438
> 
> _______________________________________________
> Cs254f11 mailing list
> Cs254f11 at lists.hampshire.edu
> https://lists.hampshire.edu/mailman/listinfo/cs254f11

--
Lee Spector, Professor of Computer Science
Cognitive Science, Hampshire College
893 West Street, Amherst, MA 01002-3359
lspector at hampshire.edu, http://hampshire.edu/lspector/
Phone: 413-559-5352, Fax: 413-559-5438



More information about the Cs254f11 mailing list