Unbound Variable or Constructor error ML - constructor

I am working with two function. One that takes a list, and the second that does some work on the list elements. The second function, multinHelp, is giving me the error
"stdIn:79.6-79.16 Error: unbound variable or constructor: multinHelp"
fun multin(L)=
let
val a = hd(L)
val b = hd(tl(L))
val c = hd(tl(tl(L)))
in
multinHelp([a,b,c])
end;
---------------------------------------
fun multinHelp([a,b,c])=
if c = 0 then [a]
else (b * hd(multinHelp([a,b,c])) :: multinHelp([a,b,c-1]);
I am not sure why I am receiving this error as I have set [a,b,c] in the first function.

You need to define the second function before the first one to be able to use it from there.
Also, there is a closing parenthesis missing in the second function.

Related

F#: Apply a value as though it is a function

Is it possible in F# to apply a value as though it is a function? For example:
let record =
{|
Func = fun x -> x * x
Message = "hello"
|}
let message = record.Message // unsweetened field access
let unsweet = record.Func 3 // unsweetened function application
let sweet = record 3 // sweetened function application
Right now, the last line doesn't compile, of course:
error FS0003: This value is not a function and cannot be applied.
Is there some sort of syntactical sweetener that would allow me to "route" function application as I see fit, while still retaining its normal unsweetened behavior as well? I'm thinking of something magic like this:
// magically apply a record as though it was a function
let () record arg =
record.Func arg
(Note: I used a record in this example, but I'd be happy with a class as well.)
The closest thing I can think of would be a custom operator using statically resolved type parameters to resolve a specific property of type FSharpFunc, then invoke the function with the supplied input parameter. Something like this:
let record =
{|
Func = fun x -> x * x
Message = "hello"
|}
let inline (>.>) (r: ^a) v =
let f = (^a : (member Func: FSharpFunc< ^b, ^c>) r)
f v
record >.> 3 // val it : int = 9

How to get a variables only defined as an argument in another function in Haskell

I have
cubicQ a b c = ((3*a*c) - b**2)/(9 * (a**2))
I need to get the value of "a" out of that so I can use it in another function, without having to pass it as an argument in that other function. The function I need to use it in is below:
cubicRealSolution q r s t = if p < 0 then error "NaN" else (s + t) - ((b)/(3*(a)))
Sorry, it's not possible. If you want a function to have access to a value, you must pass that value to the function (and it must be prepared to accept it).
Altough this won't work we'll present you this to show the idea. In Haskell you can write
cubicQ a b c = ((3*a*c) - b**2)/(9 * (a**2))
where
cubicRealSolution q r s t = if p < 0 then error "NaN" else (s + t) - ((b)/(3*(a)))
The trick is to use the where keyword. Because cubicRealSolution is wrapped inside cubicQ it can have access to its arguments a, b and c without the need to pass them.

Want to use a vector as parameter to a function, without having to separate its elements

If I call a matlab function with:
func(1,2,3,4,5)
it works perfectly.
But if I do:
a=[1,2,3,4,5] %(a[1;2;3;4;5] gives same result)
then:
func(a)
gives me:
??? Error ==> func at 11
Not enough input arguments.
Line 11 in func.m is:
error(nargchk(5, 6, nargin));
I notice that this works perfectly:
func(a(1),a(2),a(3),a(4),a(5))
How can I use the vector 'a' as a parameter to a function? I have another function otherfunc(b) which returns a, and would like to use its output as a paramater like this func(otherfunc(b)).
Comma-seperated lists
(CSL) can be passed to functions as parameter list,
so what you need is a CSL as 1,2,3,4,5 constructed from an array.
It can be generated using cell array like this:
a=[1,2,3,4,5];
c = num2cell(a);
func(c{:});
Maybe you could try with nargin - a variable in a function that has the value of the number of input arguments. Since you have a need for different length input, I believe this can best be handled with varargin, which can be set as the last input variable and will then group together all the extra input arguments..
function result = func(varargin)
if nargin == 5: % this is every element separately
x1 = varargin{1}
x2 = varargin{2}
x3 = varargin{3}
x4 = varargin{4}
x5 = varargin{5}
else if nargin == 1: % and one vectorized input
[x1 x2 x3 x4 x5] = varargin{1}
I've written x1...x5 for your input variables
Another method would be to create a separate inline function. Say you have a function f which takes multiple parameters:
f = f(x1,x2,x3)
You can call this with an array of parameter values by defining a separate function g:
g = #(x) f(x(1),x(2),x(3))
Now, if you have a vector of parameters values v = [1,2,3], you will be able to call f(v(1),v(2),v(3)) using g(v).
Just make the function take a single argument.
function result = func(a)
if ~isvector(a)
error('Input must be a vector')
end
end
Since arguments to functions in Matlab can themselves be vectoes (or even matrices) you cannot replace several arguments with a single vector.
If func expects 5 arguments, you cannot pass a single vector and expect matlab to understand that all five arguments are elements in the vector. How can Matlab tell the difference between this case and a case where the first argument is a 5-vector?
So, I would suggest this solution
s.type = '()';
s.subs = {1:5};
func( subsref( num2cell( otherfunc(b) ), s ) )
I'm not sure if this works (I don't have matlab here), but the rationale is to convert the 5-vector a (the output of otherfunc(b)) into a cell array and then expand it as 5 different arguments to func.
Please not the difference between a{:} and a(:) in this subsref.
You could create a function of the following form:
function [ out ] = funeval( f, x )
string = 'f(';
for I = 1:length(x)
string = strcat( string, 'x(' , num2str(I), '),' );
end
string( end ) = ')';
out = eval( string );
end
In which case, funeval( func, a ) gives the required output.
Use eval:
astr = [];
for i=1:length(a)
astr = [astr,'a(',num2str(i),'),']; % a(1),a(2),...
end
astr = astr(1:end-1);
eval(['func(' astr ');']);

OCaml - How do I create a function with a function as output?

"Write a function lv: cfg -> (blabel -> ide set), which computes the live variables analysis on the given control flow graph."
Having cfg and blabel defined and ide set as a list of string, how can I create a function with that signature?
You're presumably familiar with the let syntax to define a function:
let f x = x + 1 in …
You can use this syntax anywhere, including in a function body. Now if you happen to use the inner function's name as the return value of the outer function, the outer function will be returning a function.
let outer_function x =
let inner_function y = x + y in
inner_function
The let syntax is in fact syntactic sugar for fun or function. In particular, if you define inner_function just to use the name once, you might as well use the fun notation and not bother giving the inner function a name.
let outer_function x =
fun y -> x + y
Furthermore, if all the outer function does when you pass it an argument is to build and return an inner function, then consider its behavior when you pass that function two arguments. First the outer function builds an inner function, using its first (and only) argument; then that inner function is applied to the second argument, so its body is executed. This is equivalent to having just one function that takes two arguments. This
observation is known as currying.
let outer_function x y = x + y
Note that the type of this function is int -> int -> int; this is the same type as int -> (int -> int) (the arrow type operator is right-associative).
Currying doesn't apply when the outer function does some work before building the inner function. In that case, the work is performed after receiving the first argument.
let outer_function x =
print_endline "outer";
fun y -> print_endline "inner"; x + y
So the structure of your code is likely to look like this:
let lv (c : cfg) : blabel -> ide set =
let c' = do_some_precomputation c in
fun (bl : blabel) -> (… : ide set)

Multiline function calls in Coffeescript

Hi everyone: Suppose I have a function "foo" that should receive two functions as parameters. If I have two lambda functions, I can call "foo" as follows:
foo (-> 1),(-> 2)
In this case, "foo" receives two functions, one that just returns 1 and another that returns 2.
However, usually lambda functions are more complicated, so putting both functions on a single line is impractical. Instead, I would like to write two multiline lambda functions. However, I can't figure out for the life of me how to accomplish this in coffeescript- Ideally, I would want to write it as follows, but it throws an error:
foo
->
1
,
->
2
The best I can come up with that works is super ugly:
foo.apply [
->
1
,
->
2
]
Can any Coffeescript guru show me how I can do this, without getting an error? Thanks!
I believe this is one situation where anonymous functions seem to not be the answer. They are very practical and idiomatic in a lot of situations but even they have limitations and can be less readable if used in extreme situations.
I would define the two functions in variables and then use them as parameters:
func1 = ->
x = 2
y = 3
z = x+y
return z+2*y
func2 = ->
a = "ok"
return a + " if you want this way"
foo func1, func2
But if you decide lambdas would be preferable, just use the parenthesis around the parameters of foo:
foo ((->
x = 2
y = 3
z = x+y
return z+2*y
),(->
a = "ok"
return a + " if you want this way"
)
)
It is not because you are using CoffeScript that you should avoid parenthesis at any cost :)
This should suffice (you could indent the second lamda if you want):
f (->
x = 1
1 + 2 * x),
->
y = 2
2 * y
given the function f:
f = (a,b) -> a() + b()
the result should give 3 + 4 = 7
Functions are implicitly called if a variable or function follows them. That's why
foo
->
2
,
->
3
won't work; the coffeescript compiler only sees a variable followed by an unexpected indent on the next line. Explicitly calling it
foo(
->
2
, ->
3
)
will work.
You can implicitly call a function with multiple paramenters, you just need to line up the comma with the beginning of the function call
foo ->
2
, ->
3