## What is the Java equivalent of this Haskell function?

why haskell

haskell vs

haskell difficulty

haskell python

haskell performance

haskell c++

haskell tutorial

Philip Wadler's paper "Monads for functional programming" has an example of a function eval which performs division written in Haskell.

Here it is as adapted in "Programming in Haskell" by Graham Hutton:

data Expr = Val Int | Div Expr Expr eval :: Expr -> Int eval (Val n) = n eval (Div x y) = eval x `div` eval y

My Java equivalent of this is:

abstract class IntegerExpression { abstract Integer evaluate(); } class Value extends IntegerExpression { Integer value; public Value(Integer x) { value = x; } public Integer evaluate() { return value; } } class DivisionExpression extends IntegerExpression { IntegerExpression x, y; public DivisionExpression(IntegerExpression a, IntegerExpression b) { x = a; y = b; } public Integer evaluate() { return x.evaluate() / y.evaluate(); } } public class DivisionExample { public static void main(String[] args) { IntegerExpression two = new Value(2); IntegerExpression twenty = new DivisionExpression (new Value(100), new Value(5)); IntegerExpression ten = new DivisionExpression(twenty, new Value(2)); IntegerExpression five = new DivisionExpression(new Value(10), two); IntegerExpression expr = new DivisionExpression(ten, five); System.out.println(expr.evaluate()); } }

This seems fine but how do I develop this code so that I can demonstrate a Try monad (to catch division by zero) in Java?

Edit: a workaround to failures in this kind of cases is the use of the `Maybe Monad`

, and his cousin in Java is the `Optional`

class, where `Option.of`

would be `return`

and `flatMap`

would be `bind`

. On the other hand in Java and others O.O. languages there is a common pattern to use in this kind of cases called composite, basically your data type `Expr`

will be an interface or abstract class, and the type constructors will be the leaves:
So, with all of that in mind, a simple example working would be:

In haskell:

data Expr = Val Int | Div Expr Expr eval :: Expr -> Maybe Int eval (Val n) = Just n eval (Div x y) = do v1 <- eval x v2 <- eval y if v2 == 0 then Nothing else return (div v1 v2) n1 = Val 8 n2 = Val 4 n3 = Val 0 d1 = Div n1 n2 d2 = Div d1 d1 d3 = Div d2 n3 main = do putStrLn $ show (eval d2) putStrLn $ show (eval d3)

Equivalent approach in Java:

import java.util.Optional; public interface Expr { public Optional<Integer> eval(); }

Then the leaves implementing Expr:

import java.util.Optional; public class Val implements Expr{ Optional<Integer> value; public Val(int value) { this.value = Optional.of(value); } @Override public Optional<Integer> eval() { return value; } }

Then the recursive case:

import java.util.Optional; public class Div implements Expr { Expr expr1; Expr expr2; public Div(Expr expr1, Expr expr2) { this.expr1 = expr1; this.expr2 = expr2; } @Override public Optional<Integer> eval() { return expr1.eval().flatMap(v1 -> expr2.eval().flatMap(v2 -> (v2 == 0) ? Optional.empty() : Optional.of(v1 / v2) ) ); } public static void main(String[] args) { Expr iv1 = new Val(6); Expr iv2 = new Val(3); Expr iv3 = new Val(2); Expr iv4 = new Val(0); Expr div1 = new Div(iv1, iv2); Expr div2 = new Div(div1, iv3); Expr div3 = new Div(div2, iv4); System.out.println(div2.eval()); System.out.println(div3.eval()); } }

The main function output will be:

Optional[1] Optional.empty

**Haskell and Java,** parseInt(String) function throws the NumberFormatException when a Note how the Haskell equivalent to Java's Consumer involves the IO What is a good way to implement the equivalent of Haskell's “or” function using Streams in Java Tag: java , haskell , stream I'm looking for an equivalent to Haskell's "or" function in Java using Streams.

Other answers have covered a more idiomatic way of implementing this in Java, and described how to use `Optional`

to handle errors. But here I’d like to give the direct equivalent of Haskell pattern matching in Java, with the *visitor pattern*:

public class ExprTest { public static void main(String[] arguments) { // expr :: Expr // expr = Div // (Div // (Div (Val 100) (Val 5)) // (Val 2)) // (Div (Val 10) (Val 2)) Expr two = new Val(2); Expr twenty = new Div(new Val(100), new Val(5)); Expr ten = new Div(twenty, new Val(2)); Expr five = new Div(new Val(10), two); Expr expr = new Div(ten, five); // eval :: Expr -> Int // eval expr = case expr of ExprVisitor<Integer> eval = new ExprVisitor<Integer>() { // Val value -> value public Integer visit(Val val) { return val.value; } // Div left right -> eval left `div` eval right public Integer visit(Div div) { return div.left.match(this) / div.right.match(this); } }; // main = print (eval expr) System.out.println(expr.match(eval)); } } // data Expr abstract class Expr { abstract <T> T match(ExprVisitor<T> visitor); } // = Val Int class Val extends Expr { public final int value; public Val(int value) { this.value = value; } <T> T match(ExprVisitor<T> visitor) { return visitor.visit(this); } } // | Div Expr Expr class Div extends Expr { public final Expr left, right; public Div(Expr left, Expr right) { this.left = left; this.right = right; } <T> T match(ExprVisitor<T> visitor) { return visitor.visit(this); } } abstract class ExprVisitor<T> { abstract T visit(Val val); abstract T visit(Div div); }

In the land of functional programming, this is called Böhm–Berarducci encoding—sometimes referred to as Church encoding, although they’re different things. This is a fancy-sounding way of saying "representing data types and pattern matching with functions". You can of course use this encoding of matching in Haskell:

match :: (Int -> t) -- visit(Val) -> (Expr -> Expr -> t) -- visit(Div) -> Expr -> t match val div expr = case expr of Val x -> val x Div left right -> div left right eval :: Expr -> Int eval = match id (\ left right -> eval left `div` eval right)

Since `eval`

is recursive, you can also write it using the *fixed point combinator* `fix`

—and then the use of `this`

in the `ExprVisitor`

in the Java version may become more clear: it’s how you make `eval`

recursive!

import Data.Function (fix) eval :: Expr -> Int eval = fix $ \ this -> match (\ value -> value) (\ left right -> this left `div` this right)

And here’s the other half of the encoding: we can do away with the data type altogether and just use functions:

{-# LANGUAGE RankNTypes #-} newtype Expr = Expr { visit :: forall a. (Int -> a) -- Val -> (a -> a -> a) -- Div -> a } valE :: Int -> Expr valE x = Expr $ \ v _d -> v x divE :: Expr -> Expr -> Expr divE left right = Expr $ \ v d -> d (visit left v d) (visit right v d) eval :: Expr -> Int eval expr = visit expr (\ val -> val) (\ left right -> left `div` right) eval (divE (divE (divE (valE 100) (valE 5)) (valE 2)) (divE (valE 10) (valE 2))) == 2

And that implementation of `eval`

can of course be written as just this:

eval = visit expr id div

**Is Haskell's type system formally equivalent to Java's?,** Things that Java's type system has but that Haskell's doesn't: declare a parametrized bound class A t where -- provide a function via this bound tInt :: t Int Haskell is a very terse language, particularly due to its type inference. This means there's nothing to distract from the intent of the code, making it very readable. This is in sharp contrast to languages like Java, where skimming code requires learning which details can be ignored.

You could evaluate both `x`

and `y`

before the division:

Integer xE = x.evaluate(), yE = y.evaluate();

And then see if `yE`

is equal to `0`

:

if(yE == 0){ // your logic here if it is a division by 0 }

Which would yield you with the following function:

public Integer evaluate() { Integer xE = x.evaluate(), yE = y.evaluate(); if(yE == 0){ // your logic here if it is a division by 0 } return xE / yE; }

**If Java was a Haskell - The Type System,** So imagine you can create a method directly at the root of a file. Is there anything left to make a class? Yes, the data. That's why Haskell uses the 3 Functions. Since Haskell is a functional language, one would expect functions to play a major role, and indeed they do. In this section, we look at several aspects of functions in Haskell. First, consider this definition of a function which adds its two arguments: add :: Integer -> Integer -> Integer

**[PDF] Bridging the Gap between Haskell and Java,** Both Haskell and Java are meant to be general purpose programming languages. We can define an equivalent function in Haskell:. @Mysterious Dan: forbidding certain kind of state during object construction is the preferred way in Java. Otherwise you would have to invent a new “valid range” type for almost every use case of int as using the entire value range of int when using an int variable is the exception, just as an example.

**Functional programming for Java developers, Part 1,** Optimize your Java code with these five functional programming techniques. functions used in imperative programming, such as a Java method that returns a value. by Moses Schönfinkel and subsequently developed by Haskell Curry. Think of filter() as the functional equivalent of an if statement, and What is the C# equivalent of map function in Haskell. Questions: map function in Haskell has two input parameters. The first parameter is a function and second parameter is a list. The map function applies the function passed as input parameter to all the elements in the list and returns a new list.

**[PDF] A Haskell-To-Java Translation Tool,** (i.e. Java) methods from within a Haskell function, passing arguments and retrieving create similar looking (albeit not equivalent) code in Java for example,. And while it works for Scala it's a bit of a stretch to say Java has it. Haskell can also construct function types using the function constructor, ->. While Java's methods do have type signatures, you can't combine them. Java's type system does enable a type of modularity that Haskell hasn't got. It will be a while before there's an OSGi for

##### Comments

- Why not just use exceptions in your Java code? Furthermore, the Haskell code doesn't actually handle division by zero. If you're translating directly I don't see why you'd handle that.
- If you want to go functional: There is an
`Optional`

class in java - @DánielSomogyi thanks for the comment you made me understand better the problem here
- I'd use a Java interface instead of an abstract class. Also know that, in theory, any algebraic data type (like Haskell's
`data`

s) could be faithfully represented in Java using a "visitor pattern" / "impredicative encoding" / "Church encoding". Using inheritance as you do above is close, but not the same, since classes are open (new subclasses can be added). Also, it's a bit inconvenient to have to spread pattern matching over all subclasses (the visitor pattern avoids that). The main downside is that the visitor pattern is not as simple as the above approach. - Thanks, you've given me more help than I expected. I knew that the problem I had was that I hadn't chosen the correct pattern to be equivalent to the Haskell data type but you've also included an example of the use of the monad in your answer.
- @Nemo thanks so much, I have always loved to help, and this kind of issues are my favorites ones. If you have more dubts don't hesitate to write to me
- This is fairly easily adapted to use Optional<Integer> and flatmap() thanks!