Does the identity function in Clojure have the same use and purpose as the id function in Haskell?

Clojure has an identity function. This is used as follows:

 user=> (filter identity [1 2 3 nil 4 false true 1234]) (1 2 3 4 true 1234) user=> (partition-by identity (sort "abcdaabccc")) ((\a \a \a) (\b \b) (\c \c \c \c) (\d)) 

From what I see in Haskell, id used when using lenses and is used in other higher-order functions .

My question is (besides the obvious differences in the system) Does the identity function in Clojure have the same purpose and purpose as the id function in Haskell?

Why do I ask when I look at the following Lens example in Clojure - we see the id defined in terms of functor :

 (defprotocol Functor (fmap [functor f] "fmap :: fa -> (a -> b) -> fb")) ;; data Id a = Id { runId :: a } (defrecord Id [runId] Functor (fmap [functor f] (Id. (f (:runId functor))))) 

So, I feel that something is missing.

+6
source share
1 answer

The id function in Haskell is I am a combinator of lambda calculus . This is a triaxial function:

 -- | Identity function. id :: a -> a id x = x 

This is a useful way that it is useful to use 0 or an empty list.

The identity function in Clojure is equivalent to returning its argument unchanged.

 (defn identity "Returns its argument." {:added "1.0" :static true} [x] x) 

Other concepts like identity monads , etc. not directly related to id function.

+6
source

Source: https://habr.com/ru/post/975963/


All Articles