Define `id` using dot notation

In a Haskell language function id

defined as a unit of composition:

id :: a -> a
id x = x

      

Is there a way to define the haskell 'function id

using dot notation? I mean not referring to it like this:

same :: a -> a
same = id

      

+3


source to share


2 answers


The exact version id

- it's ... good id

. id

is often taken as a fundamental operator in pointless constructions, a kind of primitive on which more combinators are built.

If you really want, you can recover id

from other more complex operators, for example. using an instance Monad ((->) a)

:

id = join const

      

or an Applicative ((->) a)

instance



id = const <*> const

      

(in combinatorial logic, this I = S K K

, since <*>

there is S

also const

is K

)

Nothing could be easier than id

.

+16


source


This question is interesting, because it id

is essentially the identity of any mathematical group of functions included in the composition. Simply put, when you get two opposite functions and compose them, you get id

.

So any of them will work polymorphically and in exactly the same way, although naturally slower:

id :: a -> a
----------------------------
id = snd . (0,)
id = fst . (,0)
id = (!! 0) . return
id = ($ 0) . const
id = (\(Just n) -> n) . Just

      



Or we could write this in a simple lambda expression, which is good practice and very simple:

id = \a -> a

      

This is all in a dotless style, which should be satisfying for your question, but it is not just dotless, but glasses free. All of these solutions may seem very valid, but they are not equivalent id

. These solutions, apart from the latter, create and deconstruct data structures that are expensive in terms of performance. Glasses-free style may be good practice, but not everywhere.

+4


source







All Articles