is awesome!
A KSong Production
"Functional programming is the mustachioed hipster of programming paradigms...."
Functional programming is a style of programming which models computations as the evaluation of expressions.
From Wikipedia
-Simple English Wikipedia
a = range(10)
for i in a:
a[i] += i
/* Return the access method type of the DBObject */
static int _DB_get_type(DBObject* self)
{
DBTYPE type;
int err;
err = self->db->get_type(self->db, &type);
if (makeDBError(err)) {
return -1;
}
return type;
}
/* Create a DBT structure (containing key and data values) from Python
strings. Returns 1 on success, 0 on an error. */
static int make_dbt(PyObject* obj, DBT* dbt)
{
CLEAR_DBT(*dbt);
if (obj == Py_None) {
/* no need to do anything, the structure has already been zeroed */
}
else if (!PyArg_Parse(obj, "s#", &dbt->data, &dbt->size)) {
PyErr_SetString(PyExc_TypeError,
#if (PY_VERSION_HEX < 0x03000000)
"Data values must be of type string or None.");
#else
"Data values must be of type bytes or None.");
#endif
return 0;
}
return 1;
}
SELECT *
FROM Derps OUTER JOIN Herps
WHERE derp = false
AND herp = true
AND derpherp < 50
GROUP BY merps,perps
ORDER BY burps
LIMIT 15;
Where are the instructions?
int listSum(int* input){
int j, sum = 0;
int listsize = GETARRSIZE(input); //Magic!
for(j = 0; j < listsize; j++){
sum += input[j];
}
return sum;
}
Things to note:
int listSum(int* input){
SumObject myadder = SumObject(input);
myadder.calcSum();
return myadder.getSum();
}
Huh? That's cheating!
Well...maybe.
How is SumObject implemented?
int listSum(int* input){
if(GETARRSIZE(input) == 0){
return 0;
}
else return input[0] + listSum(input++);
}
--Haskell is sexy!
listSum :: [Int] -> Int
listSum [] = 0
listSum (x:xs) = x + listSum xs
int listSum(int* input){
if(GETARRSIZE(input) == 0){
return 0;
}
else return input[0] + listSum(input++);
}
Statements vs. Expressions
A statement alters the state of something
An expression has a value
Code can be both!
Functional programming is a style of programming which models computations as the evaluation of expressions.
last :: [a] -> a
last [] = error
last [x] = x
last (x:xs) = last xs
last ["a","b","c"] c
listLen :: [a] -> Int
listLen [] = 0
listLen (x:xs) = 1 + listLen xs
length [a,a,a,a,a] 5
element [1,1,2,3,5,8] 4 5
--Find the kth element of a list
element :: [a] -> Int -> a
element [] _ = error
element (x:xs) 0 = x
element (x:xs) k = element xs (k-1)
Prelude> let f x = x + 1
Prelude> :type f
f :: Num a => a -> a
The compiler can infer the type of the function on its own!
Prevents you from cluttering your code by spelling out the explicit types of everything.
Prelude> let dotwice f = f f
Prelude> let twotimes x = 2*x
Prelude> let h = dotwice twotimes
Prelude> h 10
40
Prelude> h 3
12
Prelude> h 6
24
--test.hs
--Attempt to update value of x
x = 5
x = 3
x++
x += 5
Any sort of loop
Passing structures by reference
(Purity)
for(int i = 0; i < a.size(); i++){
a[i] += 5;
}
for(int i = 0; i < a.size(); i++){
a[i] *= 2;
}
for(int i = 0; i < a.size(); i++){
if(a[i] %2 != 0){
a[i] = -1; //Delete value
}
}
int prod = 1;
for(int i = 0; i < a.size(); i++){
prod *= a[i];
}
map (+5) a
map (*2) a
filter even a
foldl' (*) 1 a
segLS :: [Points] -> Double -> (Double,Int)
segLS [] cost = (0,1)
segLS [_] cost = (0,1)
segLS plist cost = minimumBy (compare `on` fst)
[(costAtX x,x) | x <- [0..length plist - 1]]
where costAtX x = (fst $ segLS (take x plist) cost)
+ slsErr (drop (x) plist) + cost
double sls_topDown_memoized(double* x, double* y, double** err, int* splitpts, int maxIndex){
if(maxIndex == 0) return 0; //Offset cost for not splitting
//Costs of not splitting;
splitpts[maxIndex] = 0;
if(err[0][maxIndex] == -1){
err[0][maxIndex] = errors(0,maxIndex,x,y);
}
double minCost = err[0][maxIndex];
for(int i = 0; i < maxIndex; i++){ //Consider costs of splitting
if(err[i+1][maxIndex] == -1) err[i+1][maxIndex] = errors(i+1,maxIndex,x,y);
double costHere = sls_topDown_naive(x,y,err,splitpts,i) + err[i+1][maxIndex] + costToSplit;
if(costHere < minCost){
minCost = costHere;
splitpts[maxIndex] = i;
}
}
return minCost;
}
What's wrong with not good about this code?
static int derp = 0;
int dumbAdd(int& a, int& b){
if(derp == 0) derp = 1;
else derp = 0; //switch derp
if(derp == 0) return 0;
else return a++ + b++;
}
int a = 2, b = 3;
printf("%d\n", dumbAdd(a,b));
What does the code on the left return?
int mystery(){
derp = 0;
int a = 3, b = 2;
a = dumbAdd(a,b);
b = dumbAdd(a,b);
a = dumbAdd(a,b);
derp = 1;
b = dumbAdd(a,b);
return dumbAdd(a,b);
}
static int derp = 0;
int dumbAdd(int& a, int& b){
if(derp == 0) derp = 1;
else derp = 0;
if(derp == 0) return 0;
else return a++ + b++;
}
"Why do you do this to yourself?" --Kelvin Lwin
--isPrime is implemented elsewhere
allNums = [1..]
allPrimes = filter isPrime allNums
first1000 = take 1000 allPrimes
Find the first 1000 prime numbers
void qsort(int a[], int lo, int hi)
{
int h, l, p, t;
if (lo < hi) {
l = lo;
h = hi;
p = a[hi];
do {
while ((l < h) && (a[l] <= p))
l = l+1;
while ((h > l) && (a[h] >= p))
h = h-1;
if (l < h) {
t = a[l];
a[l] = a[h];
a[h] = t;
}
} while (l < h);
a[hi] = a[l];
a[l] = p;
qsort( a, lo, l-1 );
qsort( a, l+1, hi );
}
}
quicksort :: Ord a => [a] -> [a]
quicksort [] = []
quicksort (p:xs) = (quicksort lesser)
++ [p] ++
(quicksort greater)
where
lesser = filter (< p) xs
greater = filter (>= p) xs
--Looks for a value in a binary search tree.
Data Tree a = EmptyTree | Node Tree a Tree
target `in` EmptyTree = False
target `in` (Tree leftchild value rightchild)
| target == value = True
| target < value = target `in` leftchild
| target > value = target `in` rightchild
foldl (+) 0 [1..100000] --Stack overflow!
Though it's actually not that bad...
Difficulty: * * * * * Time: * * * * * Purity: * * * * * Practical: * *
Difficulty: * * * Time: * * * Purity: * * * Practical: * * * *
Difficulty: * * * Time: * * * Purity: * Practical: * * * * *
Difficulty: * * * Time: * * * * Purity: * * Practical: * * *
Difficulty: ? Time: ? Purity: ? Practical: ?
“But I don’t want to go among mad people," Alice remarked.
"Oh, you can’t help that," said the Cat: "we’re all mad here. I’m mad. You’re mad."
"How do you know I’m mad?" said Alice.
"You must be," said the Cat, "or you wouldn’t have come here.”