Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | |||
|
aa:lab:sol:3 [2025/10/19 21:57] andreidarlau04 [Metoda potentialelor] |
aa:lab:sol:3 [2025/10/19 21:59] (current) andreidarlau04 [Algebraic Data Types] |
||
|---|---|---|---|
| Line 148: | Line 148: | ||
| Reverse(Void) = Void | Reverse(Void) = Void | ||
| Reverse(Cons(x, L)) = Append(Reverse(L), Cons(x, Void)) | Reverse(Cons(x, L)) = Append(Reverse(L), Cons(x, Void)) | ||
| + | </code> | ||
| + | Helper: | ||
| + | <code> | ||
| + | isNull(Void) = True | ||
| + | isNull(Cons(x, L)) = False | ||
| </code> | </code> | ||
| 3. | 3. | ||
| <code> | <code> | ||
| + | bool isNull(List l) { | ||
| + | return l == NULL; | ||
| + | } | ||
| + | |||
| int size(List l) { | int size(List l) { | ||
| - | if (l == NULL) | + | if (isNull(l)) |
| return 0; | return 0; | ||
| return 1 + size(l->next); | return 1 + size(l->next); | ||
| Line 159: | Line 168: | ||
| List add(List l, int x) { | List add(List l, int x) { | ||
| - | if (l == NULL) | + | if isNull(l)) |
| return Cons(x, Void()); | return Cons(x, Void()); | ||
| return Cons(l->e, add(l->next, x)); | return Cons(l->e, add(l->next, x)); | ||
| Line 165: | Line 174: | ||
| List append(List l1, List l2) { | List append(List l1, List l2) { | ||
| - | if (l1 == NULL) | + | if (isNull(l1)) |
| return l2; | return l2; | ||
| return Cons(l1->e, append(l1->next, l2)); | return Cons(l1->e, append(l1->next, l2)); | ||
| Line 171: | Line 180: | ||
| List reverse(List l) { | List reverse(List l) { | ||
| - | if (l == NULL) | + | if (isNull(l)) |
| return Void(); | return Void(); | ||
| return append(reverse(l->next), Cons(l->e, Void())); | return append(reverse(l->next), Cons(l->e, Void())); | ||
| } | } | ||
| </code> | </code> | ||