I have two functions f and g:
let f (x:float) (y:float) =
x * y
let g (x:float) =
x * 2.0
I want to compose (>>) them to get a new function that performs f and then g on the result.
The solution should behave like:
let h x y =
(f x y) |> g
This does not work:
// Does not compile
let h =
f >> g
How should >> be used?
I think you want to achieve this:
let fog x = f x >> g
You can't compose them directly f >> g in that order, it makes sense since f expects two parameters, so doing f x will result in a partially applied function, but g expects a value, not a function.
Composing the other way around works and in your specific example you get even the same results, because you are using commutative functions. You can do g >> f and you get a composition that results in a partially applied function since g expects a single value, so by applying a value to g you get another value (not a function) and f expects two values, then you will get a partially applied function.
Writing in point-free style, i.e. defining functions without explicit arguments, can become ugly when the implicit arguments are more than one.
It can always be done, with the correct combination of operators. But the outcome is going to be a disappointment and you will lose the primary benefit of point-free style - simplicity and legibility.
For fun and learning, we'll give it a try. Let's start with the explicit (a.k.a. "pointful") style and work from there.
(Note: we're going to rearrange our composition operators into their explicit form (>>) (a) (b) rather than the more usual a >> b. This will create a bunch of parentheses, but it will make things easier to grok, without worrying about sometimes-unintuitive operator precedence rules.)
let h x y = f x y |> g
let h x = f x >> g
// everybody puts on their parentheses!
let h x = (>>) (f x) (g)
// swap order
let h x = (<<) (g) (f x)
// let's put another pair of parentheses around the partially applied function
let h x = ((<<) g) (f x)
There we are! See, now h x is expressed in the shape we want - "pass x to f, then pass the result to another function".
That function happens to be ((<<) g), which is the function that takes a float -> float as argument and returns its composition with g.
(A composition where g comes second, which is important, even if in the particular example used it doesn't make a difference.)
Our float -> float argument is, of course, (f x) i.e. the partial application of f.
So, the following compiles:
let h x = x |> f |> ((<<) g)
and that can now be quite clearly simplified to
let h = f >> ((<<) g)
Which isn't all that awful-looking, when you already know what it means. But anybody with any sense will much rather write and read let h x y = f x y |> g.
Related
Lets assume the following function
val foo : int -> int -> bool -> int
let foo x y b =
let x,y = if x > y then y,x else x,y in
let rec loop x y b =
if x >= then x
else if b then loop (x+1) y (not b)
else loop x (y-1) (not b)
in
loop x y b
I still don't quite understand the concept of the "in".
Does the line mean "let x,y = ... in" that it is executed immediately or only when you "Call" it? And when i dont need to call it why do i need the last line loop x y b?
Thanks in advance :)
in is just part of the OCaml syntax - let PATTERN = EXPR1 in EXPR2 is an expression which binds the result of EXPR1 to PATTERN and then evaluates EXPR2 with the new bindings present. In some languages like F# and Haskell, you don't (always) need in - it's inferred from the indentation. OCaml syntax is indentation insensitive which requires it to have an explicit in.
Does the line mean "let x,y = ... in" that it is executed immediately or only when you "Call" it?
It's evaluated immediately.
And when i dont need to call it why do i need the last line loop x y b?
In this code, the previous line defines a function named loop with 3 arguments, and then later you call the function with the arguments x y b.
I am trying to write a function in Julia that takes in a multi-dimensional array (a data cube) and rescales every entry from 0 to 1. However, whenever I run the code in atom, I get the error
LoadError: MethodError: no method matching -(::Array{Float64,2}, ::Float64)
Closest candidates are:
-(::Float64, ::Float64) at float.jl:397
-(::Complex{Bool}, ::Real) at complex.jl:298
-(::Missing, ::Number) at missing.jl:97
...
Stacktrace:
[1] rescale_zero_one(::Array{Float64,2}) at D:\Julio\Documents\Michigan_v2\CS\EECS_598_Data_Science\codex\Codex_3\svd_video.jl:40
[2] top-level scope at D:\Julio\Documents\Michigan_v2\CS\EECS_598_Data_Science\codex\Codex_3\svd_video.jl:50 [inlined]
[3] top-level scope at .\none:0
in expression starting at D:\Julio\Documents\Michigan_v2\CS\EECS_598_Data_Science\codex\Codex_3\svd_video.jl:48
I have the basics of what my function must do, but I really don't understand some of the notation and what the error is telling or how to fix it.
function rescale_zero_one(A::Array)
B = float(A)
B -= minimum(B)
B /= maximum(B)
return B
end
m,n,j = size(movie_cube)
println(j)
C = Array{Float64}(UndefInitializer(),m,n,j)
for k in 1:j
println(k)
C[:,:,j] = rescale_zero_one(movie_cube[:,:,j])
end
the variable movie_cube is a 3 dimensional data array of Float64 entries and I just want to rescale the entries from zero to one. However, the error that I mentioned keeps appearing. I would really appreciate any help with this code!
Try to use dot syntax for doing some operations in an array!
function rescale_zero_one(A::Array)
B = float.(A)
B .-= minimum(B)
B ./= maximum(B)
return B
end
This code is a bit faster and simpler (it only makes two passes over the input matrix rather than five in the previous answer):
function rescale(A::Matrix)
(a, b) = extrema(A)
return (A .- a) ./ (b - a)
end
This can be generalized to three dimensions, so that you don't need the outer loop over the dimensions in C. Warning: this solution is actually a bit slow, since extrema/maximum/minimum are slow when using the dims keyword, which is quite strange:
function rescale(A::Array{T, 3}) where {T}
mm = extrema(A, dims=(1,2))
a, b = first.(mm), last.(mm)
return (A .- a) ./ (b .- a)
end
Now you could just write C = rescale(movie_cube). You can even generalize this further:
function rescale(A::Array{T, N}; dims=ntuple(identity, N)) where {T,N}
mm = extrema(A, dims=dims)
a, b = first.(mm), last.(mm)
return (A .- a) ./ (b .- a)
end
Now you can normalize your multidimensional array along any dimensions you like. Current behaviour becomes
C = rescale(movie_cube, dims=(1,2))
Rescaling each row is
C = rescale(movie_cube, dims=(1,))
Default behaviour is to rescale the entire array:
C = rescale(movie_cube)
One more thing, this is a bit odd:
C = Array{Float64}(UndefInitializer(),m,n,j)
It's not wrong, but it is more common to use the shorter and more elegant:
C = Array{Float64}(undef, m, n, j)
You might also consider simply writing: C = similar(movie_cube) or C = similar(movie_cube, Float64).
Edit: Another general solution is to not implement the dimension handling in the rescale function, but to rather leverage mapslices. Then:
function rescale(A::Array)
(a, b) = extrema(A)
return (A .- a) ./ (b - a)
end
C = mapslices(rescale, A, dims=(1,2))
This is also not the fastest solution, for reasons I don't understand. I really think this ought to be fast, and might be sped up in a future version of Julia.
Since Ocaml utilizes type inferences, and functions are a type, do I have to use fun to declare a function? For example, the REPL/interpreter for Ocaml executes this statement without complaint:
let average a b =
(a +. b) /. 2.0;;
Does this mean that I can dispense with fun when declaring functions? When is it needed, or is it ever needed?
The keyword fun is needed with anonymous functions. For instance, the following code doubles each elements of the list l = [ 1; 2; 3; 4]
let l = [1; 2; 3; 4]
let doubled_l = List.map (fun x -> 2 * x) l
but this snippet could be rewritten as
let l = [1; 2; 3; 4]
let double x = 2 * x
let doubled_l = List.map double l
( or even List.map ( ( * ) 2 ) l )
Contrarily, your average function could be rewritten as
let average = fun x y -> (x +. y) /. 2.
or
let average = fun x -> fun y -> (x +. y) /. 2.
(the syntax average x y = ... is in fact a syntactic sugar for this form)
let average a b = ... is exactly equivalent to let average = fun a b -> ... - you can always use the former over the latter. It's just a matter of style which one you prefer. My feeling is that most programmers use the former form, but some introductory materials use the latter form to make it clear that functions are values just like any other and can be created using the fun keyword.
However let f = fun ... is not the only way to use fun. You can use it wherever a value of a function type is expected, for example as an argument to map. So if you have something like List.map (fun x -> x+1) xs, you can't just put a let in place of the fun because a definition makes no syntactic sense there. You could of course use let add1 x = x+1 in List.map add1 xs and that works fine, but that's more verbose and introduces a new name that's only used once and doesn't really add anything.
So fun is useful when you want to pass a function around without giving it a name.
I have a function f:
f = Function('f')(x,y).
The output of my program is a large polynomial with terms XYf, Xf, Yf for variables X and Y. I would like to define the substitution such that
X f(x,y) = f(x+1,y)
Y f(x,y) = f(x,y+1)
Similarly, XY f(x,y) = f(x+1,y+1).
I have used the following code to define the operation of X and Y.
poly = poly.subs(X*f, f.subs(x,x+1))
poly = poly.subs(Y*f, f.subs(y,y+1))
Though this works with terms of Xf and Yf, it does not work with terms of XYf. XYf gives the output as Yf(x+1,y) instead of f(x+1,y+1).
How do I force Y to act on the "new" f?
XYf gives Yf(x+1, y) because it matches Xf and that's the first substitution you do. To replace all three in the way that you want, you should do them in an order such that you don't match later instances, like
poly = poly.subs(X*Y*f, f.subs(x,x+1).subs(y, y + 1))
poly = poly.subs(X*f, f.subs(x,x+1))
poly = poly.subs(Y*f, f.subs(y,y+1))
That way, you replace all X*Y*f(x, y) first, so when you replace X*f(x, y) and Y*f(x, y) it won't replace X*Y*f(x, y) (because they will already be replaced).
As a side note, in terms of code clarity, it's going to be simpler if you just define
f = Function('f')
and then explicitly write f(x, y), f(x + 1, y), and so on (rather than letting f = f(x, y) and using subs to create f(x + 1, y) and so on).
I'm having trouble composing functions in ML
local
fun add(g:int->int,f:int->int,m:int,x:int,sum:int):int=
if m>x then sum
else add(g,g o f,m+1,x,sum+(g f x))
in
fun sum f g x=
if x=0 then f x
else add(g,g o f,1,x,f x)
end;
now, what I'm trying to do is basically f(x)+g(f(x))+g(g(f(x)))+...+g^x(f(x))
but I can't get it right. I keep getting an error that operand and operator don't agree for g o f. can someone point out what I'm doing wrong and maybe how can I fix it?
fun sum f g x
is a given signature of a function so I can't change it.
I don't think the error is in g o f, it's in g f x. That does not type-check, because g is not a function of two arguments. Did you mean g(f x) there, perhaps?