Lec 1: functional programming 1
Notes on setting up your IDE
I have found the answer to the issue I was having. This reddit response helped me (https://www.reddit.com/r/Racket/comments/l1mar4/comment/gk0qfe6/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button). Basically if you are following the CS61A course by Bryan Harvey then he uses a specific version of Scheme which can be included into DrRacket IDE by putting the following at the start of your script and selecting "Determine language from source":
#lang racket
(require (planet dyoo/simply-scheme:2:2))
The following stack overflow question helped me out here... (https://stackoverflow.com/a/19203899/13470111)
Scheme Programming Language
Scheme is a programming language from the Lisp family. It's authors are Gerald Jay Sussman & Guy L. Steele. It supports functional programming.
You ask it a question: 6
It gives you an answer: 6
Structure of Scheme's syntax
Maths uses a few types of notation for operations. Here are some examples:
- Infix:
- Prefix:
- Postfix:
- All-around:
Scheme uses prefix notation only, so that it is consistent. Using prefix only means less repetitions of the operator symbol.
Q: (+ 6 8)
A: 14
Variable numbers of arguments
The built-in mathematical prefix operators can have any number of arguments*.
Q: (+ 6 5 7 9)
A: 27
Q: (+)
A: 1
*Not all operators can be supplied with any number of arguments I.e. Division cannot take 0 arguments.
What happens when operators are not enclosed in parenthesis?
=> +
;Value: #[arity-dispatched-procedure 1]
The representation of the function is displayed. The value of the symbol is a function. Just as is a function.
=> sin
;Value: #[compiled-procedure 2 ("arith" #x140) #x14 #x10a2abc4c]
Symbols as values
You may need to use the symbol as a value not as the built-in function it represents.
To achieve this, prefix the symbol with a single quote. The value of the following "word" (Schemes name for string) becomes simply that word. The quote escapes the symbols' representation as a function.
=> '+
;Value: +
Composition of functions
Taking the result of 1 function and using it's result as an argument for another function. The inner most functions are evaluated first.
=> (+ (* 3 5) (* 10 11))
;Value: 125
The most powerful tool for organizing computation.
Functions without numbers
These functions are provided with the planet dyoo/simply-scheme:2:2
library which provides the same functions which are used within the Berkley lectures.
Here are some examples of word based functions.
(first 'hello)
h
(last 'hello)
o
(butfirst 'hello)
ello
(butlast 'hello)
hell
Creating a sentence is achieved by surrounding a group of words with parenthesis and prefixing that with a single quote.
(first '(got to get you into my life))
Giving names to things
More accurately, creating definitions. Similar to other languages where you create variables.
Here I define a short version of pi. Print it's value and use it within another function.
(define pi 3.1415)
pi
(* pi 5 5)
3.1415
78.53750000000001
Defining procedures
In other languages, procedures might be called "functions". They are defined in the same way we defined pi above.
- define keyword
- name of procedure
- arguments
- function body
- use the function
(define (square x)
(* x x))
(square (+ 2 3))
25
Define is a special form.
- It is special because is does not evaluate the arguments.
- If it's a function the value's do not get evaluated
(define hello (+ 2 3))
hello
(define (hello2) (+ 2 3))
hello2
(hello2)
The first definition, when called will evaluate the body.
The second definition, when called will not evaluate the body unless called as a function i.e. wrapped in parenthesis.
5
#<procedure:hello2>
5
The Parts of a Function
A function has many parts.
- NAME
- FORMAL PARAMETER
- BODY
- ARGUMENT EXPRESSION
- ACTUAL ARGUMENT VALUE
Here are the individual definitions for the parts of a function.
- NAME: The name is the what comes after the define keyword.
The name is used to call the function.
- FORMAL PARAMETER: A formal parameter is the name for an argument to a function. They can be thought of as placeholders.
The x in:
(define (square(x))
- BODY: The body is what evaluates when the function is called.
The contents of the final inner parenthesis :
(define (square(x))
(* x x))
- ARGUMENT EXPRESSION: Argument expressions are evaluated to give you the argument values. Which is then used as the argument in the function, providing a value for the formal parameter. Switching the placeholder with a real value.
This is called composition of functions (+ 2 3) is evaluated to 5 which is used as the square function formal parameter.
Composition of functions is one of the most important ways we organize code.
(square(+ 2 3))
- ACTUAL ARGUMENT VALUE: evaluation of what is within the parenthesis after a function name.
This is (+ 2 3) evaluated:
(square(5))
Example
'computer
is the argument expression and is the argument value.
(plural 'computer)
'computers
Building a function
Making a function which returns the plural of a word.
(require (planet dyoo/simply-scheme:2:2))
(define (plural wd)
(word wd 's))
(plural 'computer)
(plural 'book)
(plural 'boy)
(plural 'fly)
computers
books
boys
flys
There is a problem with this because the word "fly" becomes "flys" when instead ot should become "flies".
Let's try to fix it.
(define (plural wd)
(if (equal? (last wd) 'y))
(word (bl wd) 'ies)
(word wd 's)))
Predicates and convention
equal?
is a predicate function which returns true or false. It is a convention that they have a question mark at the end. It just help you read what it is doing.
Plural procedure
All works apart from we have broken boys.
'computers
'books
'flies
'boies
TODO: Fix this as part of my project for this chapter.