Arc Forumnew | comments | leaders | submitlogin
Ambiguity in function with multiple arguments
1 point by bOR_ 5939 days ago | 4 comments
How do I tell arc that a a function which accepts 3 arguments that it shouldnt try to evaluate its arguments before they are passed to the function?

  (myfun (functionthatreturnsalist xint)
  )
should not try to feed myfun the xint'th entry of the list that is the first argument.

Thanks in advance! =)



3 points by drcode 5939 days ago | link

In Lisp, function parameters are always evaluated. However, many commands in Lisp are not true "functions", but instead special forms or macros.

You probably mean to ask one of two other questions:

Q: How do I pass a literal list into a function?

A: By quoting it (This is probably not what you're trying to achieve though)

   (myfun '(functionthatreturnsalist xint))
Q: How do I write a command that can perform manipulations on parameters before evaluating them?

A: You write a macro, not a function. (See the pg arc tutorial for examples)

-----

1 point by bOR_ 5939 days ago | link

I'll put in a bit more detail., hopefully enough. It might be that I indeed need a macro, but it could also be some oversight in me.

Given the function

  (def dot (g x y)
     (* (g 0) x) + (* (g 1) y))
(which evidently expects g to be a list, and x and y to be numbers), I get this error message when supplying it with variables.

dot((list 0 -1 1) 0.3 0.3) #<procedure: dot> arc> Error: "list-ref: expects type <non-negative exact integer> as 2nd argument, given: 0.3; other arguments were: (0 -1 1)"

-----

3 points by rntz 5939 days ago | link

You can't use "foo(bar)" to call foo on bar in arc, you have to say "(foo bar)". Interpreting "dot((list 0 -1 1) 0.3 0.3)", Arc first evaluates 'dot (doing nothing) and then evaluates ((list 0 -1 1) 0.3 0.3), which calls the newly created list '(0 -1 1) on the two arguments 0.3 and 0.3, which in turn causes an error because it's trying to get the 0.3rd element from the list, which makes no sense. (Also it's been given two arguments when it expects only one.) This is a case of Arc not having very informative error messages.

Also, I think there's an error in the 'dot function, as you use a '+ where it will do nothing. Arc has no infix math, so what you want is something like this:

    (def dot (g x y)
      (+ (* (g 0) x) (* (g 1) y)))
    
    (dot (list 0 -1 1) 0.3 0.3)

-----

1 point by bOR_ 5939 days ago | link

arg. that's a silly error of mine. I translated the program from ruby, and that one must have slipped.

-----