Arc Forumnew | comments | leaders | submitlogin
2 points by Pauan 4436 days ago | link | parent

"I don't get this either."

I didn't at first either. But I walked through the steps one-by-one until I figured it out. Here they are:

apply is a function, so it evaluates all its arguments. When calling (apply list '((1 2))) its arguments evaluate to the function list and the list ((1 2)). It then calls the function list with the argument (1 2). This is equivalent to (list (1 2)), as rocketnia said.

---

To put it another way, (apply list '((1 2))) would be equivalent to (eval (cons list '((1 2)))) which is equivalent to (eval (list list '(1 2))) which is equivalent to (eval '(list (1 2))) which is equivalent to (list (1 2)).

Thus, you would either need to say (apply list '('(1 2))) or apply would need to map quote over the arguments list.



1 point by Pauan 4436 days ago | link

In fact, following that line of thought, here's an implementation of apply:

  (def apply (f args env)
    (eval (cons f (map quote args)) env))
You can make the env argument optional if you like, with whatever semantics you want (the current environment, a new environment, etc.)

---

Side note: why doesn't Arc 3.1 use this definition? Because eval is incredibly slow in Racket, but applying a function is fast. But I'm assuming in a language that emphasizes fexprs (like Kernel, or wart) that eval should be plenty fast.

-----

2 points by rocketnia 4436 days ago | link

  >
  > (map quote args)
  >
I don't think that does what you think it does. Suppose 'map is implemented independently of 'apply, and that it takes this form:

  (def map (f seq)
    ...
    ... (f elem) ...
    ...)
Then (map quote '(1 2 3)) should result in the list (elem elem elem). That's what my intuition says anyway, not that it's really useful behavior. :-p

Mapping [list quote _], as I was talking about when I called it ($lambda (x) (list $quote x)), will hopefully work regardless of what mapping quote does.

-----

1 point by Pauan 4436 days ago | link

Right, I was assuming quote automagically read my mind and did what you're talking about. :P

-----