DEV Community

林子篆
林子篆

Posted on • Originally published at dannypsnl.github.io on

1

NOTE: bounded polymorphism

Bounded polymorphism refers to existential quantifiers(∃), restricted to range over types of bound type. To understand it only needs a few examples. Let’s start! Take a look at the following program:

numSort :: Num a => [a] -> [a]
Enter fullscreen mode Exit fullscreen mode

Num a is how we represent the bounded polymorphism in Haskell , the definition of Num was class Num b where(Hoogle shows a, just prevent to confuse reader don’t familiar with Haskell ) could read as a type b is an instance of class Num.

So numSort takes [a] only if a is an instance of Num. Now we could run down:

numSort [1, 2, 3] :: [Int]
numSort [1.1, 2, 3] :: [Double]
Enter fullscreen mode Exit fullscreen mode

This is really a powerful feature(and you don’t need to use Haskell for this, Java also has this feature), consider the old way to do List<A> to List<B>, and unfortunately solution was to copy each element in the list.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (1)

Collapse
 
dannypsnl profile image
林子篆

forgot that MathJax expression xd. Did anyone know how to use LaTeX in dev.to?

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay