Lists of Functions and their execution Erlang - functional-programming

Is it possible to create and send a list of functions as an argument to another function, and then have some functions within this list call other functions in this list?
For example, I want a function that works on a list passed as an argument, and then performs the first function in the list of functions on this list of numbers, and if that function makes calls to other functions within that list, they can be retrieved and used.
e.g.: deviation(Numbers, Functions) -> %Functions = [fun master/1, fun avg/1, fun meanroots/1]
Master calls avg, then passes that result into meanroots, etc. but at the end of the call chain the master is the one that will return a value.
I'd like to know if this is functionally possible, whether within OTP alone or using NIFs, and if there are samples of implementation to look at.

How would your function know whether one of the functions in the list called another function in the list? I think your question is worded confusingly.
You could create a function to chain the results through a series of functions:
chain(Nums, []) -> Nums;
chain(Nums, [H | T]) -> chain(H(Nums), T).
which could be done with a standard function:
lists:foldl(fun (F, V) -> F(V) end, Nums, Funcs)

Of course you can:
1> F1 = fun(A) -> A*A end.
#Fun<erl_eval.6.50752066>
2> F2 = fun(A) -> A+A end.
#Fun<erl_eval.6.50752066>
3> F3 = fun(A) -> A*A+A end.
#Fun<erl_eval.6.50752066>
4> Funs = [F1, F2, F3].
[#Fun<erl_eval.6.50752066>,#Fun<erl_eval.6.50752066>,
#Fun<erl_eval.6.50752066>]
5> [F(X) || X <- [1,2,3], F <- Funs].
[1,2,2,4,4,6,9,6,12]
You could create tagged tuples with functions, e.g. {tag1, F1} (where F1 is defined as above), pass them to functions and do with them all sort of stuff you would normally do with any other variable in Erlang.

Related

Is recursive partial function application possible in F#?

I am working with the function 2x^2+y^2, defined in my code as:
fx = fun x y -> (2 * (pown x 2)) + (pown y 2)
and I was wondering if it was possible to apply the x and y components recursively?
Currently, if I do, say,
let f1 = fx 0.0 //<fun:it#9-2>
let f2 = f1 2.0 //4.0
then this works as expected. However, if I were to give my function a and b parameters to indicate some interval, intellisense throws a fit.
let ab = [0.0; 2.0]
let result =
let rec getres fn lst =
match lst with
| hd :: tl ->
let f1 = fn hd
getres f1 tl // <- error here
| [] -> fn
getres fx ab
then intellisense on f1 gives me an error:
Type mismatch. Expecting a
"a -> 'b'
but given a
"b'.
The types "a' and "b -> 'a' cannot be unified
I want to be able to recursively apply any number of parameters from a list of parameters (e.g. my ab list in the example above) to a function that I supply to my expression, in the form of fun x1 x2 x3 … xn -> x1 + x2 + x3 + … + xn. Is this possible?
I think you may need to specify a signature for the function argument of getres. Based on the error message the compiler is inferring that fn takes a single argument and returns a result. So on the error line f1 is not a function but a value. Ie. there is no currying happening.
But that won't handle the final case where you want to execute the function rather than performing partial application. You may need an extra level of indirection to handle this. (In other .NET languages one would need to use reflection for this – which allows a function to be called passing an array of parameters – it may not be possible in a strongly typed .NET language to handle this without reflection.)
Additional (to summarise the comments below):
Consider the required signature of getres. If fn is a function of two arguments (ie. 'a -> 'a -> 'b) then getres has a signature:
('a -> 'a -> 'b) -> list 'a -> ('a -> 'b`)
for the first call.
But when that instance of getres makes its recursive call it needs to be a function taking a single argument, ie. its signature needs to be:
('a -> 'b) -> list 'a -> 'b`
This is not possible with a single F# function.
It is possible with a .NET function (ie. a class member), but only if each "level" is separately written (ie. hard coding the maximum number of arguments). (Compare how System.Func<T1, TRes>, System.Func<T1, T2, TRes> are defined in the .NET runtime.)
(This is the kind of thing that dynamic languages can easily handle, but strongly typed languages need a very sophisticated type system underlying it to achieve.)

SMLNJ powerset function

I am trying to print the size of a list created from below power set function
fun add x ys = x :: ys;
fun powerset ([]) = [[]]
| powerset (x::xr) = powerset xr # map (add x) (powerset xr) ;
val it = [[],[3],[2],[2,3],[1],[1,3],[1,2],[1,2,3]] : int list list;
I have the list size function
fun size xs = (foldr op+ 0 o map (fn x => 1)) xs;
I couldnt able to merge these two functions and get the result like
I need something like this:
[(0,[]),(1,[3]),(1,[2]),(2,[2,3]),(1,[1]),(2,[1,3]),(2,[1,2]),(3,[1,2,3])]
Could anyone please help me with this?
You can get the length of a list using the built-in List.length.
You seem to forget to mention that you have the constraint that you can only use higher-order functions. (I am guessing you have this constraint because others these days are asking how to write powerset functions with this constraint, and using foldr to count, like you do, seems a little constructed.)
Your example indicates that you are trying to count each list in a list of lists, and not just the length of one list. For that you'd want to map the counting function across your list of lists. But that'd just give you a list of lengths, and your desired output seems to be a list of tuples containing both the length and the actual list.
Here are some hints:
You might as well use foldl rather than foldr since addition is associative.
You don't need to first map (fn x => 1) - this adds an unnecessary iteration of the list. You're probably doing this because folding seems complicated and you only just managed to write foldr op+ 0. This is symptomatic of not having understood the first argument of fold.
Try, instead of op+, to write the fold expression using an anonymous function:
fun size L = foldl (fn (x, acc) => ...) 0 L
Compare this to op+ which, if written like an anonymous function, would look like:
fn (x, y) => x + y
Folding with op+ carries some very implicit uses of the + operator: You want to discard one operand (since not its value but its presence counts) and use the other one as an accumulating variable (which is better understood by calling it acc rather than y).
If you're unsure what I mean about accumulating variable, consider this recursive version of size:
fun size L =
let fun sizeHelper ([], acc) = acc
| sizeHelper (x::xs, acc) = sizeHelper (xs, 1+acc)
in sizeHelper (L, 0) end
Its helper function has an extra argument for carrying a result through recursive calls. This makes the function tail-recursive, and folding is one generalisation of this technique; the second argument to fold's helper function (given as an argument) is the accumulating variable. (The first argument to fold's helper function is a single argument rather than a list, unlike the explicitly recursive version of size above.)
Given your size function (aka List.length), you're only a third of the way, since
size [[],[3],[2],[2,3],[1],[1,3],[1,2],[1,2,3]]
gives you 8 and not [(0,[]),(1,[3]),(1,[2]),(2,[2,3]),...)]
So you need to write another function that (a) applies size to each element, which would give you [0,1,1,2,...], and (b) somehow combine that with the input list [[],[3],[2],[2,3],...]. You could do that either in two steps using zip/map, or in one step using only foldr.
Try and write a foldr expression that does nothing to an input list L:
foldr (fn (x, acc) => ...) [] L
(Like with op+, doing op:: instead of writing an anonymous function would be cheating.)
Then think of each x as a list.

How to recursively make a list of lists in SML/NJ

I'm brand new to SML/NJ and I'm trying to make a recursive function
that makes a listOfLists. Ex: listOf([1,2,3,4]) will output
[[1],[2],[3],[4]]. I've found a recursive merge in SML/NJ, and I'm
trying to use it as kind've an outline:
- fun merge(xs,nil) = xs
= | merge(nil,ys) = ys
= | merge(x::xs, y::ys) =
= if (x < y) then x::merge(xs, y::ys) else y::merge(x::xs,ys);
- fun listOf(xs) = xs
= | listOf(x::xs) = [x]::listOf(xs);
I'm trying to use pattern match and I'm a little confused on it. I'm
pretty sure x is the head and then xs is the tail, but I could be
wrong. So what I'm trying to do is use the head of the list, make it a
list, and then add it to the rest of the list. But when trying to do
this function, I get the error:
stdIn:15.19-15.34 Error: operator and operand don't agree [circularity]
operator domain: 'Z list * 'Z list list
operand: 'Z list * 'Z list
in expression:
(x :: nil) :: listOf xs
This error is foreign to me because I don't have really any experience
with sml/nj. How can I fix my listOf function?
You are fairly close. The problem is that in pattern-matching, a pattern like xs (just a variable) can match anything. The fact that you end it with s doesn't mean that the pattern can only match a tail of a list. Using s in that way is just a programmer convention in SML.
Thus, in your definition:
fun listOf(xs) = xs
| listOf(x::xs) = [x]::listOf(xs);
The first line tells SML to return all values unchanged, which is clearly not your intent. SML detects that this is inconsistent with the second line where you are trying to change a value after all.
You need to change that first line so that it doesn't match everything. Looking at that merge function as a template, you need something which matches a basis case. The natural basis case is nil (which can also be written as []). Note the role that nil plays in the definition of merge. If you use nil instead of xs for the pattern in the first line of your function definition, your second line does exactly what you want and the function will work as intended:
fun listOf(nil) = nil
| listOf(x::xs) = [x]::listOf(xs);

Memoisation in OCaml and a Reference List

I am learning OCaml. I know that OCaml provides us with both imperative style of programming and functional programming.
I came across this code as part of my course to compute the n'th Fibonacci number in OCaml
let memoise f =
let table = ref []
in
let rec find tab n =
match tab with
| [] ->
let v = (f n)
in
table := (n, v) :: !table;
v
| (n', v) :: t ->
if n' = n then v else (find t n)
in
fun n -> find !table n
let fibonacci2 = memoise fibonacci1
Where the function fibonacci1 is implemented in the standard way as follows:
let rec fibonacci1 n =
match n with
| 0 | 1 -> 1
| _ -> (fibonacci1 (n - 1)) + (fibonacci1 (n - 2))
Now my question is that how are we achieving memoisation in fibonacci2. table has been defined inside the function fibonacci2 and thus, my logic dictates that after the function finishes computation, the list table should get lost and after each call the table will get built again and again.
I ran some a simple test where I called the function fibonacci 35 twice in the OCaml REPL and the second function call returned the answer significantly faster than the first call to the function (contrary to my expectations).
I though that this might be possible if declaring a variable using ref gives it a global scope by default.
So I tried this
let f y = let x = ref 5 in y;;
print_int !x;;
But this gave me an error saying that the value of x is unbounded.
Why does this behave this way?
The function memoise returns a value, call it f. (f happens to be a function). Part of that value is the table. Every time you call memoise you're going to get a different value (with a different table).
In the example, the returned value f is given the name fibonacci2. So, the thing named fibonacci2 has a table inside it that can be used by the function f.
There is no global scope by default, that would be a huge mess. At any rate, this is a question of lifetime not of scope. Lifetimes in OCaml last as long as an object can be reached somehow. In the case of the table, it can be reached through the returned function, and hence it lasts as long as the function does.
In your second example you are testing the scope (not the lifetime) of x, and indeed the scope of x is restricted to the subexpresssion of its let. (I.e., it is meaningful only in the expression y, where it's not used.) In the original code, all the uses of table are within its let, hence there's no problem.
Although references are a little tricky, the underlying semantics of OCaml come from lambda calculus, and are extremely clean. That's why it's such a delight to code in OCaml (IMHO).

How many arguments does the map function take in SML?

I'm trying to learn SML and some of my professor notes talk about the map function "which has type ('a -> 'b) -> ('a list -> 'b list)." He goes on to explain that this means that "you give me a function that transforms 'a s into 'b s...".
However, he the implementation looks like:
fun map f [] = []
| map f (a::l) = (f a)::(map f l)
This appears to me that it's taking the equivalent of 2 arguments (I know that everything in sml technically only takes one argument, but using tuples or currying it can look like 2). It appears that it's taking a function and a list. However, the explanation above makes it sound like it's only taking a function. What am I missing?
As you said yourself SML doesn't really have polyadic functions, it only simulates using tuples or currying. In this case map is a curried function. The reason that your professors statement makes sense is that a curried function is simply a function that takes one argument and returns a function that takes the next argument.
One way to see this is to rewrite fun in terms of val. In general fun f x y = bla is equivalent to val rec f = fn x => fn y => bla. So the definition of map can equivalently be written like this:
val rec map = fn f => fn as => case as of
[] => []
| (a::l) => (f a)::(map f l)
Looking at that definition it's clear that the first fn creates a function that returns another function (the second fn).
We can also see that this is the case by observing that it is legal to call map with only one argument and the result is a function:
val my_map = map my_function
val mapped_list = my_map my_list
In the above my_map my_list is equivalent to map my_function my_list. This shows us that calling map my_function my_list (which is equivalent to (map my_function) my_list due to function application being left-associative) simply calls map with the argument my_function and then calls the function retuned by map with my_list as its argument.
That's what currying is all about.

Resources