leafriend
leafriend

Reputation: 105

Using function like OOP method in Haskell

I'm studying Haskell these days, and got a idea using function like OOP method.

First, define a operator as below:

(//) :: a -> (a -> b) -> b
x // f = f x

As you can see, this operator reverses the order of function f and argument x then applies it. For example, equals can be defined as:

equals :: Eq a => a -> a -> Bool
equals = \x -> \y -> x == y

comparison = (1 + 2) // equals 3 -- True

Now here is my question. Is it good approach using Haskell function like OOP method? That means inversion of function and (first) argument is good or not.

UPDATE

Here is the case of backtick(`) is not available.

data Person = Person { name :: String }
myself = Person "leafriend"

then

> name myself
"leafriend"
> myself // name
"lefirend"
> myself `name`
ERROR - Syntax error in expression (unexpected end of input)

Upvotes: 1

Views: 311

Answers (1)

Dan Burton
Dan Burton

Reputation: 53705

What you have written is merely a matter of style. (It seems rather cruel to C/Java programmers to use // as an operator, though.) I would discourage using this for a few reasons, mainly because:

  • It's not idiomatic Haskell, and will confuse any Haskellers trying to read your code
  • You shouldn't think of Haskell as you would think of an OOP language

If you're trying to make Haskell look more like your favorite OOP language, you're doing it wrong. Haskell is meant to look like math: kind of lisp-y, with prefix function application, but also with infix functions and operators available.

Upvotes: 2

Related Questions