Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
pp:rec [2017/02/24 16:24] pdmatei created |
pp:rec [2019/02/17 23:42] (current) dmihai add reading |
||
---|---|---|---|
Line 27: | Line 27: | ||
#include <stdlib.h> | #include <stdlib.h> | ||
#include <assert.h> | #include <assert.h> | ||
+ | |||
#define NODE_NO 3 | #define NODE_NO 3 | ||
// graph represented as adjacency matrix | // graph represented as adjacency matrix | ||
Line 33: | Line 33: | ||
int** m; | int** m; | ||
int nodes; | int nodes; | ||
- | } Graph; | + | }* Graph; |
+ | |||
Graph make_graph (int** m, int n){ | Graph make_graph (int** m, int n){ | ||
- | Graph g; | + | struct Graph* g = (struct Graph*)malloc(sizeof(struct Graph)); |
- | g.m = m; | + | g->m = m; |
- | g.nodes = n; | + | g->nodes = n; |
return g; | return g; | ||
} | } | ||
- | + | ||
- | int visited[NODE_NO]; | + | int visited[NODE_NO] = {0,0,0}; |
+ | |||
void visit(Graph g, int from){ | void visit(Graph g, int from){ | ||
visited[from] = 1; | visited[from] = 1; | ||
printf(" Visited %i ",from); | printf(" Visited %i ",from); | ||
int i; | int i; | ||
- | for (i = 0; i<g.nodes; i++){ | + | for (i = 0; i<g->nodes; i++){ |
- | if (g.m[from][i] && !visited[i]) | + | if (g->m[from][i] && !visited[i]) |
visit(g,i); | visit(g,i); | ||
} | } | ||
} | } | ||
+ | |||
+ | int main (){ | ||
+ | |||
+ | int** a = (int**)malloc(NODE_NO*sizeof(int*)); | ||
+ | a[0] = (int*)malloc(NODE_NO*sizeof(int)); | ||
+ | a[0][0] = 0; a[0][1] = 1; a[0][2] = 0; | ||
+ | a[1] = (int*)malloc(NODE_NO*sizeof(int)); | ||
+ | a[1][0] = 0; a[1][1] = 0; a[1][2] = 1; | ||
+ | a[2] = (int*)malloc(NODE_NO*sizeof(int)); | ||
+ | a[2][0] = 0; a[2][1] = 0; a[2][2] = 0; | ||
+ | |||
+ | Graph g = make_graph((int**)a,3); | ||
+ | visit(g,0); | ||
+ | |||
+ | //visit(g,1); | ||
+ | |||
+ | } | ||
+ | |||
</code> | </code> | ||
Line 204: | Line 222: | ||
if (i == n) | if (i == n) | ||
return f2; | return f2; | ||
- | f1 = f2; | + | int nf1 = f2; |
- | f2 = f1+f2; | + | int nf2 = f1+f2; |
- | i = i+1; | + | int ni = i+1; |
- | n = n; //this instruction may be later eliminated by subsequent optimisation stages. | + | int nn = n; //this instruction may be later eliminated by subsequent optimisation stages. |
+ | f1 = nf1; | ||
+ | f2 = nf2; | ||
+ | i = ni; | ||
+ | nn = n; | ||
goto start; | goto start; | ||
} | } | ||
Line 290: | Line 312: | ||
op (a, op (b, op (c, fold (i, op, [])))) | op (a, op (b, op (c, fold (i, op, [])))) | ||
op (a, op (b, op (c, i))) | op (a, op (b, op (c, i))) | ||
- | a op (b op ( c op i)) | + | a op (b op (c op i)) |
</code> | </code> | ||
Line 300: | Line 322: | ||
tail_fold(op(b,op(a,i)),op, [c]) | tail_fold(op(b,op(a,i)),op, [c]) | ||
tail_fold(op(c,op(b,op(a,i))),op,[]) | tail_fold(op(c,op(b,op(a,i))),op,[]) | ||
- | ((c op b) op a) op i | + | c op (b op (a op i)) |
</code> | </code> | ||
Line 349: | Line 371: | ||
<code haskell> | <code haskell> | ||
foldr op acc [] = acc | foldr op acc [] = acc | ||
- | foldr op acc (x:xs) = op (foldr op acc xs) | + | foldr op acc (x:xs) = op x (foldr op acc xs) |
</code> | </code> | ||
Line 375: | Line 397: | ||
<code haskell> | <code haskell> | ||
reverse l = foldl (:) [] l | reverse l = foldl (:) [] l | ||
- | append l1 l2 = foldl (:) l2 l1 | + | append l1 l2 = foldr (:) l2 l1 |
</code> | </code> | ||
+ | ===== Recommended Reading ===== | ||
+ | |||
+ | * [[http://worrydream.com/refs/Hughes-WhyFunctionalProgrammingMatters.pdf|Why Functional Programming Matters]] | ||
+ | * [[https://wiki.haskell.org/Why_Haskell_matters|Why Haskell Matters]] |