Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to write Haskell's flip function in Rust?

Tags:

haskell

rust

Haskell's flip function is defined as follows:

flip :: (a -> b -> c) -> b -> a -> c
flip f x y =  f y x

All it does is take a function and return another function that has its two parameters reversed. If you called a function f as f a b (f(a, b) in Rust-like syntax), you would call flip f as (flip f) b a.

My unsuccessful attempt at writing this in Rust:

fn flip<A, B, C, F: Fn(A, B) -> C>(f: F) -> impl Fn(B, A) -> C {
    |a, b| f(b, a)
}

Is it possible to write this in Rust?

like image 248
Listerone Avatar asked May 01 '19 01:05

Listerone


People also ask

What is a flip function?

flip() function. The flip() function is used to reverse the order of elements in an array along the given axis. The shape of the array is preserved, but the elements are reordered.

What does flip function do in Haskell?

Flip simply takes a function and returns a function that is like our original function, only the first two arguments are flipped. We can implement it like so: flip' :: (a -> b -> c) -> (b -> a -> c)

How do you use until in Haskell?

until condition and while condition do ... statements. while stops when the condition is false, until stops when the condition is true. In the same language, two different conventions.

What do rust and Haskell have in common?

Both languages embrace the idea of explicitly marking things. For example, both languages encourage (in Haskell's case) or enforce (in Rust's case) marking the type signature of all functions. But that's pretty common. Haskell goes further, and requires that you mark all effectful computations with the IO type (or something similar, like MonadIO ).

What is Haskell programming language?

Haskell is defined as a functional programming language that is general-purpose, powerful, and reliable language and hence it particularly suited for fast prototyping, proprietary business logic, enhancing the current environment of the software with properly written code and data analysis.

What is the overall philosophy of Haskell?

In the Haskell world, the overall philosophy is generally to approach "if it compiles, it works." Haskellers love enforcing almost every invariant at the type level.

Why does Haskell have such odd syntax for functions?

This combination turns many common structures in other languages (like loops) into normal functions. But in order to make this feel natural, Haskell uses slightly odd (compared to other languages) syntax for function application.


1 Answers

I'm not sure what error you're getting, but I was able to implement it properly with this code (playground link for testing):

fn flip<A, B, C, F>(f: F) -> impl Fn(B, A) -> C where F: Fn(A, B) ->C {
    move |a,b| f(b, a)
}

fn main() {
    let sub = |a, b| a - b;
    let flipped = flip(sub);
    println!("Result: {}", flipped(5, 10));
}

Note the move keyword here, which is required to force the closure returned by flip to take ownership of f.

like image 50
apetranzilla Avatar answered Oct 19 '22 10:10

apetranzilla