Lecture 3 - Polymorphism

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/11

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

12 Terms

1
New cards

What are guards?

Guards can be use to constrain when equations can match (aka. When an expression evaluates to a Bool value), as an alternative to conditional statements.

<p><span>Guards can be use to constrain when equations can match (aka. When an expression evaluates to a Bool value), as an alternative to conditional statements.</span></p>
2
New cards

What is Haskell’s chosen version of polymorphism?

Parametric.

3
New cards

What is parametric polymorphism? How does Haskell implement this?

Haskell uses “generic programming” or parametric polymorphism, and implements this through a single definition of a function that applies generically and identically to values of any type using type variables.

4
New cards

What is a type variable?

Variables representing any type.

5
New cards

What other types of polymorphism do OO languages implement?

  • Ad-hoc - Where we overload functions by writing multiple implementations, one for each type.

  • Subtype - We relate datatypes by using a supertype instance for all subtypes.

6
New cards

What is overloaded polymorphism?

Sometimes we want to restrict the number of types a polymorphic function can use. We do this by establishing class constraints in the type definition like “(+) :: Num a => args” which ensures that that function can only use instance types of that class.

7
New cards

So, what is a (type) class?

A group of types supporting certain collective methods.

8
New cards

So, what is a (type) instance?

A type within a type class is an instance of that class which can implement those methods.

9
New cards

So, what is a (type) interface?

A method that all instances of that type class should support. Haskell allows us to add interfaces to existing types and vice versa, which is very difficult in other languages.

10
New cards

What are uncurried functions?

Uncurried functions have multiple arguments packaged in tuples/lists. Arguments are taken one at a time.

11
New cards

What are curried functions?

‘Currying’ is the process of breaking down a multi-argument function into a series of single-argument functions. Type annotation associates to the right [Int -> Int -> Int = Int -> (Int -> Int)], and function application associated to the left [mult x y z = ((mult x) y) z.

12
New cards

Can you compare equality of functions?

No. To check if two functions are equal, you’d have to apply them to every possible input and check that their outputs are always equal. For functions with infinite domains (like Int → Int), this is undecidable, so not computable in general.