06_AccDiff_p1 - Accumulators & Difference Lists York...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Accumulators & Difference Lists York University Department of Computer Science and Engineering York University- CSE 3401- V. Movahedi 1 06_AccDiff
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Overview Accumulators Length of a list Sum of list of numbers Reverse a list Factorial Parts problem Difference Lists Parts problem Reverse a list [ref.: Clocksin- Chap.3 and Nilsson- Chap. 7] [also Prof. Gunnar Gotshalks’ slides] York University- CSE 3401- V. Movahedi 2 06_AccDiff
Background image of page 2
Using accumulators Useful when we calculate a result depending on what we find while traversing a structure, e.g. a list Example: Finding the length of a list Example: listlen([a, b, c], 3) Without accumulator: listlen([], 0). listlen([X|L], N) :- listlen(L, N1), N is N1 + 1. Recursively makes the problem smaller, until list is reduced to empty list On back substitution, the counter is added up. York University- CSE 3401- V. Movahedi 3 06_AccDiff
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
(without) Accumulators Without accumulators: C0: listlen([], 0). C1: listlen([X|L], N) :- listlen(L, N1), N is N1 + 1. Recursive search: G0: :- listlen([a,b,c], N). Res. w C1
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/14/2012 for the course CSE 3401 taught by Professor Movahedi during the Fall '11 term at York University.

Page1 / 11

06_AccDiff_p1 - Accumulators & Difference Lists York...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online