Unable to use function passed in as parameter? - lisp

I am trying to use a function (secondaryFunction or secondaryFunction2) passed in as a parameter to primaryFunction. However, when I run my code below:
(defun secondaryFunction (param1 param2)
NIL
)
(defun secondaryFunction2 (param1 param2))
NIL
)
(defun primaryFunction (transition param1 param2)
(transition param1 param2)
)
(primaryFunction 'secondaryFunction 0 0)
I get the following error:
*** - EVAL: undefined function TRANSITION
This seems strange, considering that I thought that I passed in secondaryFunction clearly as the transition parameter to primaryFunction?

There are separate function and value namespaces. In primary-function, you get the transition function as a value (actually the symbol here). Use funcall to call it:
(defun primary-function (transition param1 param2)
(funcall transition param1 param2))
Side Note: You are using a symbol, which means that funcall calls the function with that symbol as a name in the global environment. That's OK. You can also pass the function itself, with the function special operator (which has a shorthand #' reader macro):
(primary-function #'secondary-function 0 0)

Related

The negative and positive predicates

I refer to 17.5.7.4 Predicates and see the demonstration:
— Function: zerop x
Returns true if x is numerically zero, in any of the Calc data types. (Note that for some types, such as error forms and intervals,
it never makes sense to return true.) In defmath, the expression ‘(= x
0)’ will automatically be converted to ‘(math-zerop x)’, and ‘(/= x
0)’ will be converted to ‘(not (math-zerop x))’.
However, it report error when apply it
ELISP> (math-zerop 0)
*** Eval error *** Symbol’s function definition is void: math-zerop
ELISP> (math-zerop 1)
*** Eval error *** Symbol’s function definition is void: math-zerop
What's the problem?
Emacs tries to lazy load (called autoloading) some of its features. math-zerop is defined as part of the calc-misc feature (in calc-misc.el).
You can load it by (require 'calc) which loads the calc-misc feature.

Why does one of these semingly equivalent macros fail?

Consider these two macro definitions:
macro createTest1()
quote
function test(a = false)
a
end
end |> esc
end
macro createTest2()
args = :(a = false)
quote
function test($args)
a
end
end |> esc
end
According to the builtin Julia facilities they should both evaluate to the same thing when expanded:
println(#macroexpand #createTest1)
begin
function test(a=false)
a
end
end
println(#macroexpand #createTest2)
begin
function test(a = false)
a
end
end
Still I get a parse error when trying to evaluate the second macro:
#createTest2
ERROR: LoadError: syntax: "a = false" is not a valid function argument name
It is a space in the second argument list. However, that should be correct Julia syntax. My guess is that it interprets the second argument list as another Julia construct compared to the first. If that is the case how do I get around it?
The reason that the second macro is failing as stated in my question above. It looks correct when printed however args is not defined correctly and Julia interprets it as an expression which is not allowed. The solution is to instead define args according to the rules for function parameters. The following code executes as expected:
macro createTest2()
args = Expr(:kw, :x, false)
quote
function test($(args))
a
end
end |> esc
end

Is there an emacs lisp splat operator or another way of performing this type of operation?

I have an operator that operates on a list of variables like so:
(myfunc arg1 nil arg2 arg3)
I need to optionally (dependent on a boolean variable which we'll call my_bool) add an extra argument to this list so that the function call will look like this:
(myfunc arg1 nil arg2 added_argument arg3)
My first thought was to do this with an if block like so:
(myfunc arg1 nil arg2 (if mybool t nil) arg3)
but that will result in a nil if mybool is equal to nil which is not allowed by the function syntax.
My second thought was that maybe we could filter the list for nil and remove it, but we aren't able to because of the nil that occurs first (or rather I can't think of a way to, I'd be happy to be proven wrong, just looking for a solution).
My third thought was if we had a splat operator like Ruby's we could filter and then splat and all would be sorted out, so like so:
(myfunc arg1 nil (mysplat arg2 (filter (!= nil) (if mybool t nil)))) arg3)
but I haven't been able to find a splat operator (more technically a list destructuring operator) (P.S. the code above is approximate since it's a solution that doesn't work so I'm sure there .
So my problem is how to optionally add a single argument to a function call and not produce a nil as that argument if we do not.
Thanks in advance, sorry for my rookie emacs lisp skills :(.
You can use the splice operator ,# inside backquotes:
`(arg1 nil arg2 ,#(if my-bool (list added-argument) nil) arg3)
That is, if my-bool is true, then splice the list created by (list added-argument) in the middle of the list, and otherwise splice in nil - which is the empty list, and therefore doesn't add any elements. See the Backquote section of the Emacs Lisp manual for more information.
(And once you've created this list, it looks like you'll want to use apply to call myfunc with a variable number of arguments: (apply 'myfunc my-list))

Dynamic variables in perl

I am wondering about how I can do in Perl what I commonly do in lisp:
(defvar *verbose-level* 0)
(defun my-function (... &key ((:verbose-level *verbose-level*) *verbose-level*) ...) ...)
this means that my-function is run at the current level of verbosity, but I can pass it a different level which will affect all its calls too:
(defun f1 (&key ((:verbose-level *verbose-level*) *verbose-level*))
(format t "~S: ~S=~S~%" 'f1 '*verbose-level* *verbose-level*)
(f2 :verbose-level 1)
(format t "~S: ~S=~S~%" 'f1 '*verbose-level* *verbose-level*)
(f2 :verbose-level (1+ *verbose-level*))
(format t "~S: ~S=~S~%" 'f1 '*verbose-level* *verbose-level*))
(defun f2 (&key ((:verbose-level *verbose-level*) *verbose-level*))
(format t "~S: ~S=~S~%" 'f2 '*verbose-level* *verbose-level*))
[17]> (f1)
F1: *VERBOSE-LEVEL*=0
F2: *VERBOSE-LEVEL*=1
F1: *VERBOSE-LEVEL*=0
F2: *VERBOSE-LEVEL*=1
F1: *VERBOSE-LEVEL*=0
NIL
[18]> (f1 :verbose-level 4)
F1: *VERBOSE-LEVEL*=4
F2: *VERBOSE-LEVEL*=1
F1: *VERBOSE-LEVEL*=4
F2: *VERBOSE-LEVEL*=5
F1: *VERBOSE-LEVEL*=4
(note that the variable bindings are restored on exit - even abnormal - from functions).
How can I do something like that in Perl?
E.g., in misc.pm, I have our $verbose=0;.
How do I write a function which would bind $verbose to a value of its argument and restore its value on return?
Perl's concept of global variables is quite similar to special variables in CL.
You can “shadow” the value of a global variable with local:
our $var = 1;
func("before");
{
# a block creates a new scope
local $var = 2;
func("inside");
}
func("after");
sub func { say "#_: $var" }
Output:
before: 1
inside: 2
after: 1
If you local a value, the new value is visible throughout the whole dynamic scope, i.e. in all functions that are called. The old value is restored once the lexical scope is left by any means (errors, returns, etc). Tail calls do not extend the dynamic scope, but count as scope exit.
Note that global variables have a fully qualified name. From a different package, you would do something like
local $Other::Package::var = 3;
Other::Package::func("from a package far, far away");
This is commonly used to provide configuration for packages with a functional (non-OO) interface. Important examples are
Carp
and
Data::Dumper.
If I understand you correctly, you need to locally override a global variable inside a function.
package my_package;
our $verbose = 0;
sub function {
my ($arg1, $arg2) = #_; # getting function arguments.
local $verbose = $arg1;
}
It will restore old state of $verbose on return.

Calling a Clojure function with string inside swap?

The macro, transform!, as defined below seems to work for => (transform! ["foo" 1 2 3]). The purpose is to take in a list, with the first element being a string that represents a function in the namespace. Then wrapping everything into swap!.
The problem is that transform! doesn't work for => (transform! coll), where (def coll ["foo" 1 2 3]). I am getting this mystery exception:
#<UnsupportedOperationException java.lang.UnsupportedOperationException: nth not supported on this type: Symbol>
The function:
(defmacro transform!
" Takes string input and update data with corresponding command function.
"
[[f & args]] ;; note double brackets
`(swap! *image* ~(ns-resolve *ns* (symbol f)) ~#args))
I find it strange that it works for one case and not the other.
Macros work at compile-time and operate on code, not on runtime data. In the case of (transform! coll), the macro is being passed a single, unevaluated argument: the symbol coll.
You don't actually need a macro; a regular function will suffice:
(defn transform! [[f & args]]
(apply swap! *image* (resolve (symbol f)) args)))
Resolving vars at runtime could be considered a code smell, so think about whether you really need to do it.
You're passing a symbol to the macro, namely coll. It will try to pull that symbol apart according to the destructuring statement [f & args], which won't be possible of course.
You can also use (resolve symbol) instead of (ns-resolve *ns* symbol).