-
Notifications
You must be signed in to change notification settings - Fork 4
0097: Email Tutorials – How to think recursively
Bernard Sibanda edited this page Dec 17, 2025
·
1 revision
- Name the function (short, conventional names in Haskell).
- Write the type (types guide the design).
- Enumerate cases (write the “skeleton” by pattern matching).
- Define the simple cases (often base cases).
- List ingredients you can use (parameters, the function itself, useful operators/constants).
- Define the other cases (often recursive case(s), reduce the problem size).
-
Think about the result: can you generalize the type? can you simplify the definition (wildcards, merging cases, using
foldr, etc.)?
sum' [] = ...
sum' (x:xs) = ...Empty list sums to 0 (identity for +).
Add head to sum of tail:
sum' :: [Int] -> Int
sum' [] = 0
sum' (x:xs) = x + sum' xsGeneralize beyond Int, and recognize it’s a fold:
sum' :: Num a => [a] -> a
sum' = foldr (+) 0drop' 0 [] = ...
drop' 0 (x:xs) = ...
drop' n [] = ...
drop' n (x:xs) = ...- Dropping
0returns the list unchanged. - Dropping from
[]returns[](design choice: don’t crash).
To drop n>0 from (x:xs), drop n-1 from xs.
drop' :: Int -> [a] -> [a]
drop' 0 xs = xs
drop' _ [] = []
drop' n (_:xs) = drop' (n-1) xsGoal: init [1,2,3] = [1,2].
The “easy” moment is when the tail is empty — i.e., the list has exactly one element.
init' :: [a] -> [a]
init' [_] = []
init' (x:xs) = x : init' xsAnd since the single element isn’t used:
init' :: [a] -> [a]
init' [_] = []
init' (x:xs) = x : init' xsinit [] is still possible at the type level; the problem statement forbids it (and the real Prelude init errors on []).
Bernard Sibanda is a global Technology Entrepreneur, Web3 and Software Consultant with a deep focus on Cardano Blockchain, Midnight and Community building.
Key Positions:
- Founder, CTO, Developer Advocate cohort #1, Fullstake Developer, Cardano Ambassador, Catalyst Project Manager, DREP-WIMS:
- Co-founder of ABL Tech and Cardano Africa Live
- EBU-certified Plutus Pioneer (Plutus/Haskell)
- Cohort #1 Plutus Pioneer Developer
- Catalyst Community Reviewer & Funded Projects Manager
-
DRep for WIMS-Cardano (ID:
drep1yguj8zu48n99pv70yl6ckzt9hdgjy8yjnlqs2uyzcpafnjgu4vkul) - Intersect Developer Advocate
- Intersect Committe Member 2025-2026
- Cardano Marketer,Promoter and blogger
- Cardano Open Source Contributor
- Cardano communities and events organizer and builder
- Cardano Ambassador for South Africa
Official links:
- Stablecoins Dex
- Coxygen Global Universities
- WIMS Cardano Global
- Cardano Africa Live
- WIMS Cardano Videos
- Cardano Smart Contract Videos
- Fullstack IT Consulting
Social links: