0% found this document useful (0 votes)
26 views

1.2.functions Recursion

This document discusses functions and recursion in Scheme. It explains that in Scheme, unlike languages like C or Java, functions are values that can be passed as arguments or returned as results. This allows for powerful abstraction using functions as parameters and higher-order functions. It also discusses recursion and how to process recursive data structures using induction and pattern matching. The methodology for writing functions is outlined as understanding the problem, writing tests, then implementing the solution by analyzing cases through structural induction and pattern matching.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

1.2.functions Recursion

This document discusses functions and recursion in Scheme. It explains that in Scheme, unlike languages like C or Java, functions are values that can be passed as arguments or returned as results. This allows for powerful abstraction using functions as parameters and higher-order functions. It also discusses recursion and how to process recursive data structures using induction and pattern matching. The methodology for writing functions is outlined as understanding the problem, writing tests, then implementing the solution by analyzing cases through structural induction and pattern matching.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Basics

2. Functions and Recursion


Contents

01 Functions as values

02 Methodology

03 Recursion + Induction

04 Pattern Matching
What are functions in Scheme?
Execute any function you’ve learned without the parentheses. e.g.

What is the result?


Does it report an error?

No, but in languages as C or Java, it


would be an error. In those
languages, the functions are NOT
values. However in many
languages, functions ARE
VALUES. This is a powerful feature
Functions as parameters
What is it useful for?

ABSTRACTION, let’s think on the following problem, tell the logic of the
solution

With an array [“earth”, “mars”, “neptune”, “jupiter”] as parameter, return an


array with the character count of each word: [5,4,7,7].
Map

From: 9 Functionals | Advanced R and 9 Basic map functions


Functions as parameters
What is it useful for?

ABSTRACTION, let’s think on the following problem, tell the logic of the
solution

Parting of a list [1,2,3,4,5] obtain the summation of all the values.


Fold l/r (reduce)

From: Drawing foldl and foldr – Blog – Joachim Breitner's Homepage


Other functions - Homework
With the list ‘(1 2 3 4 5) execute the following expressions and say, why do we obtain
this result?

(foldl cons '() my-list)


(foldr cons '() my-list)

Define, using foldl a function (reject lst pred) which returns the list of the elements of
lst that do not satisfy the condition pred.

Write one example of functions that allow filtering and searching

Use of the previous seen in the real world →


MapReduce: Simplified Data Processing on Large Clusters – Google Research
Anonymous Functions
Sometimes, we may need functions that are not crucially used by different methods and that execute
some expressions. There, we may use a function with no name.

When a function receives another one as argument, is called higher order function
Functions that return functions
So, with anonymous functions we can do something awfully powerful, define functions that create
functions.

Define function (curry f) that given a function (A B → C) returns an equivalent


function A → (B → C).

> (define cons1 ((curry cons) 1))


> (map cons1 '(a b c d))
'((1 . a) (1 . b) (1 . c) (1 . d))
Methodology for function writing
So, functions are powerful, how we should write them?

1. Understand what is the use of the function.


2. Write the contract (parameters + types,
return type).
3. Write the description of the function in a
small comment.
4. Write meaningful unit tests before anything
else.
5. Implement the function body.
Backus-Naur Form → BNF
How to process recursive structures?

Induction → To establish properties.

So to demonstrate that P(n) is valid for any n (where n is a natural number). You
need to prove that:

- P(0) : P is applicable for 0 (or base case, which may vary)


- P(n) ⇒ P(n+1) : If P is applicable for n, it should be applicable for the next
n+1.
Induction + Recursion
induction (mathematical logic) ~= recursion (programming)
(Curry-Howard correspondence)
Induction + Recursion
induction (mathematical logic) ~= recursion (programming)
(Curry-Howard correspondence)
Induction + Recursion

This generalization is called STRUCTURAL


INDUCTION. So the methodology we saw earlier is
completed by splitting the last step into two:

1. Understanding the process, write the recursive


pattern.
2. Complete the body function, analyzing the cases
Pattern Matching

So, to work with data structures, we need to identify patterns.

To do so, we consider every constructor of the data structure as a


case.

But matching in Racket is wider, for lists, pairs, vectors, tons of


things
Summary

● Functions are values (as arguments or return


values). This is powerful.
● Use the methodology, do not rush on implementing,
that is the last step.
● Follow the grammar, search for patterns.
On next class

● Parsing
● Expressions
● Our first little language
References

● PrePLAI, sections 3 and 4.


● Racket Guide
● Racket Reference

You might also like