Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an inverse of the Haskell $ operator?

A quick question, is there an operator in Haskell that works like the dollar sign but gives precedence to the left hand side. I.E. instead of

f (x 1)  

being written as

f $ x 1 

I'd like to write it as

x 1 $ f 

This is purely a stylistic thing. I'm running a sequence of functions in order and it would be nice if I could write them left to right to match that I read left to right. If there an operator for this?

[update] A couple of people have asked if I can't define my own. In answer, I wanted to check there wasn't an existing operator before I reinvented the wheel.

like image 522
Benjamin Confino Avatar asked Nov 03 '10 18:11

Benjamin Confino


People also ask

What does the operator do in Haskell?

Haskell provides special syntax to support infix notation. An operator is a function that can be applied using infix syntax (Section 3.4), or partially applied using a section (Section 3.5).

What does == mean in Haskell?

The == is an operator for comparing if two things are equal. It is quite normal haskell function with type "Eq a => a -> a -> Bool". The type tells that it works on every type of a value that implements Eq typeclass, so it is kind of overloaded.

Is there and operator in Haskell?

In Haskell we have or operator to compare the values of the variable, this operator also comes under the lexical notation of the Haskell programming language. This operator works in the same way as any other programming language, it just returns true or false based on the input we have provided.

What is cons operator in Haskell?

The : operator is known as the "cons" operator and is used to prepend a head element to a list. So [] is a list and x:[] is prepending x to the empty list making a the list [x] . If you then cons y:[x] you end up with the list [y, x] which is the same as y:x:[] .


Video Answer


2 Answers

As of GHC 7.10 (base 4.8.0.0), & is in Data.Function: https://hackage.haskell.org/package/base-4.8.0.0/docs/Data-Function.html

like image 51
FranklinChen Avatar answered Oct 02 '22 12:10

FranklinChen


In Haskell you can use flip to change arguments' order of any binary function or operator:

ghci> let (|>) = flip ($) ghci> 3 |> (+4) |> (*6) 42 
like image 29
sastanin Avatar answered Oct 02 '22 12:10

sastanin