wk 5: adt's maybe either

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/20

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 7:40 PM on 4/17/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

21 Terms

1
New cards

what do adts let us do

define our own description data models with multiple forms

2
New cards

adt example

data Shape

= Circle Float

| Rectangle Float Float

| Square Float

3
New cards

what does pattern matching with adts allow us to define

it allows us to define how a function behaves for each possible form of the data

4
New cards

pattern matching with adts example

area :: Shape -> Float

area (Circle r) = pi r r

area (Rectangle w h) = w * h

area (Square s) = s * s

5
New cards

recursive adts

a type can be defined in terms of itself

6
New cards

recursive adt example

data IntList

= Empty

| Cons Int IntList

This mirrors how lists work in Haskell. The list [1,2,3] is shorthand for 1 : (2 : (3 : [])).

sumList :: IntList -> Int

sumList Empty = 0

sumList (Cons x xs) = x + sumList xs

7
New cards

polymorphism

allows us to write functions that work across many types

8
New cards

benefits of polymorphism

increases code reuse and reduces redundancy

9
New cards

polymorphism example

length :: [a] -> Int

identity :: a -> a

identity x = x

10
New cards

type classes

allow us to restrict polymorphism to types that support certain operations

11
New cards

common type classes

eq, ord, show

12
New cards

typeclass example

isEqual :: Eq a => a -> a -> Bool

isEqual x y = x == y

maxOfTwo :: Ord a => a -> a -> a

maxOfTwo x y = if x > y then x else y

13
New cards

combining polymorphism and type classes example

describe :: (Eq a, Show a) => a -> a -> String

describe x y =

if x == y

then "They are equal: " ++ show x

else "They are different: " ++ show x ++ " vs " ++ show y

14
New cards

maybe error handling

use to encode possibilities that may fail or may not return a value

15
New cards

maybe example

safeDiv :: Int -> Int -> Maybe Int

safeDiv _ 0 = Nothing

safeDiv x y = Just (x div y)

safeHead :: [a] -> Maybe a

safeHead [] = Nothing

safeHead (x:_) = Just x

16
New cards

either error handling

either allows us to return info about what went wrong

17
New cards

either example

safeDivMsg :: Int -> Int -> Either String Int

safeDivMsg _ 0 = Left "Division by zero!"

safeDivMsg x y = Right (x div y)

18
New cards

module

collection of relation functions, types, and typeclasses

19
New cards

define a haskell program in terms of modules

main module loads up the other modules and then uses the functions deifned in them to do something

20
New cards

what does show do

converts values of a type to a striing

21
New cards

what does read do

convert strings to values of our type