recursion and higher order

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

1/14

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 10:19 AM on 6/4/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

15 Terms

1
New cards

recursion

programming technique where you create a function to solve a complex problem, calling itself with smaller/simpler input using a base case and recursive case.

2
New cards

base case

simplest possible input where the answer is known directly and doesn’t need any more recursive calls

3
New cards

recursive case

part of recursion where the function is broken down into smaller instances and calls itself

4
New cards

purpose of recursion; why preferred over while/for loops

  • some data structures like folders are naturally recursive since they contain files/other folders and trying to track these structures iteratively with loops is error prone

  • sorting problems or searching pathways are clearer when expressed recursively

5
New cards

how does recursion work internally?

when a function is called, python pauses the current execution and pushes a stack frame(data block) onto the call stack which stores the function’s local variables and parameters. each recursive call results in a new stack frame, with its own local copy of parameters. when a base case is triggered, the topmost frame returns a value and is taken off the stack, and the program unwinds down to the initial call.

6
New cards

what happens in the case of infinite recursion?

it pushes stack frames indefinitely, but since memory is finite, python reaches its safety ceiling and raises a recursion error saying that maximum recursion depth has been exceeded

7
New cards

if a local variable changes in a recursive call, does it change for all previous calls?

no, each frame isolates its own variables

8
New cards

what does doing number//10 do

chops off the last digit off number

9
New cards

what does [1:] do

cuts off the first digit in a list

10
New cards

higher order function

a function that either accepts one or more functions as an argument, or returns a function as an output

11
New cards

purpose of higher order functions

functions are “first class citizens”(have no restrictions and can be treated as standard values like ints and strings) and higher order functions result in more generic, dynamic code blocks that adjust themselves based off the input(instead of you having to manually code for each input)

12
New cards

lambda functions

created without names(anonymous) and is meant to be used where it is created(throwaway), for single brief tasks(isn’t intended to be re-used) and is deleted after the line runs

13
New cards

what is the syntax for a lambda function

lambda [parameters]: [expression]; cannot contain loops or action statements like print

14
New cards

when are lambdas typically used? what is their purpose

passed as arguments inside higher-order functions, keeps code clean and concise

15
New cards

function factories, and closure

functions that generate and return new customized functions dynamically, based on given arguments. with closure, when an inner function is defined inside an outer function, it holds onto the outer functions variables even after the outer function has been executed and is removed from the call stack.