How to use (.) in Haskell

user3928256

I'm trying to write something like this in Haskell:

length . nub . intersect

but it doesn't work.

*Main Data.List> :t intersect
intersect :: Eq a => [a] -> [a] -> [a]
*Main Data.List> :t nub
nub :: Eq a => [a] -> [a]
*Main Data.List> :t length
length :: [a] -> Int

Based on the type, my understanding is that intersect returns a type of [a] and donates to nub , which takes exactly a type of [a] , then also returns a type of [a] to length , then finally the return should be an Int. What's wrong with it?

Carsten

The problem here is that intersect takes 2 arguments (in a sense)

you can provide one of the arguments explicitly:

> let f a = length . nub . intersect a
> :t f
f :: Eq a => [a] -> [a] -> Int

or you can use a fun little operator like (.:) = (.) . (.):

> let (.:) = (.) . (.)
> :t length .: (nub .: intersect)
length .: (nub .: intersect) :: Eq a => [a] -> [a] -> Int

here is a version where you don't need the parens:

import Data.List

infixr 9 .:

(.:) :: (c -> d) -> (a -> b -> c) -> a -> b -> d
(.:) = (.).(.)

f :: Eq a => [a] -> [a] -> Int
f = length .: nub .: intersect

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to use the "Of" lenses? (Haskell)

From Dev

How to use (.) in Haskell

From Dev

Haskell - How to use <- in where clauses

From Dev

How to use Haskell's `HashMap`?

From Dev

Haskell: How to use this data structure

From Dev

How to use 'aux' keyword in Haskell

From Dev

How to (install and) use package in haskell

From Dev

How to use foldr correctly in Haskell?

From Dev

How to use $ for avoiding parenthesis in Haskell

From Dev

How to use Filter and Map Haskell?

From Dev

Haskell: how to use case function

From Dev

How to use and initialize a variable haskell

From Dev

How to use catch inside an ErrorT in Haskell?

From Dev

How to properly use Haskell's `parseTimeM` function?

From Dev

Functionally solving questions: how to use Haskell?

From Dev

How to use length annotated lists in Haskell

From Dev

How to use multiple 'commands' in the same function in Haskell

From Dev

How to define and use global array in Haskell?

From Dev

Don't know how to use . and $ operator in Haskell

From Dev

How can I use getLine or getChar in haskell?

From Dev

How to correct use derived Read method in haskell

From Dev

How to properly use Haskell's `parseTimeM` function?

From Dev

Don't know how to use toEnum in Haskell

From Dev

how to use Special(reserved) characters in haskell?

From Dev

How would I use lens in Haskell to duplicate Python's enumerate?

From Java

How to use the argument of a function in a high order function in Haskell

From Dev

How do I use a Haskell type constructor as an enumeration?

From Dev

How do you use a range in haskell with a variable distance between elements

From Dev

How to properly use monadic expressions in Haskell without getting parse errors?

Related Related

HotTag

Archive