0% found this document useful (0 votes)
19 views3 pages

csc330h2 5w25

The document is a homework assignment for a programming languages course focused on writing Prolog predicates using recursion. It includes exercises for implementing various mathematical and list operations such as factorial, sum, Fibonacci, and list manipulation functions. The assignment emphasizes the use of recursion and error checking with built-in predicates, while also requiring the implementation of certain functions without built-ins.

Uploaded by

gotoh32
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

csc330h2 5w25

The document is a homework assignment for a programming languages course focused on writing Prolog predicates using recursion. It includes exercises for implementing various mathematical and list operations such as factorial, sum, Fibonacci, and list manipulation functions. The assignment emphasizes the use of recursion and error checking with built-in predicates, while also requiring the implementation of certain functions without built-ins.

Uploaded by

gotoh32
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CSC330 W25 Programming Languages – Homework §2.

5 Page 1/3

Homework §2.5 – Prolog Recursion

In Exercises 1-6, write the given Prolog predicate using recursion. For error checking, use the
built-in predicate integer/1, which succeeds if and only if its argument is an integer.
1. factorial/2, where the second argument is matched with the factorial value of the
first argument. For example:
?- factorial(4,R).
R = 24
2. sum/2, where the first argument is a non-negative integer and the second argument is
matched with the sum of all the non-negative integers up through and including the
given value. For example:
?- sum(5,R).
R = 15
3. recfun/2, where the first argument is the input of the function below, and the second
argument is matched with the output of the function.
1, 𝑛𝑛 = 0
𝑓𝑓(𝑛𝑛) = �
2 ∗ 𝑓𝑓(𝑛𝑛 − 1), 𝑛𝑛 ≥ 1
For example:
?- recfun(0,R).
R = 1
?- recfun(5,R).
R = 32
4. fib/2, where the first argument is the input of the function below, and the second
argument is matched with the output of the function.
1, 𝑛𝑛 = 0
𝑓𝑓(𝑛𝑛) = � 1, 𝑛𝑛 = 1
𝑓𝑓(𝑛𝑛 − 1) + 𝑓𝑓(𝑛𝑛 − 2), 𝑛𝑛 ≥ 2
For example:
?- fib(2,R).
R = 2
?- fib(6,R).
R = 13
5. gcd/3, where the first two arguments are non-negative integers, and the third
argument is matched with their greatest common divisor. For example:
?- gcd(28,14,R).
R = 7
6. isOdd/1 and isEven/1, where the argument is a non-negative integer. They should
succeed or fail corresponding to the parity of the integer. The predicates should also be
mutually recursive. For example:
?- isOdd(4).
false
?- isEven(4).
true
CSC330 W25 Programming Languages – Homework §2.5 Page 2/3

In Exercises 7-20, write the given Prolog predicate using recursion. Do not use any built-in
predicates unless otherwise stated. You may assume the inputs are valid.
7. myLength/2, where the second argument is matched with the length of the list in the
first argument. For example:
?- myLength([a,[],[a,b,c,d]],R).
R = 3
8. myMember/2, which succeeds when the element in the first argument occurs as a
member of the list given in the second argument. For example:
?- myMember(a,[b,c,a,d]).
true
9. myAppend/3, where the third argument is matched with the concatenation of the two
lists given in the first two arguments. For example:
?- myAppend([a,b],[c,d,e],R).
R = [a,b,c,d,e]
10. myReverse/2, where the second argument is matched with the reversal of the list
given in the first argument. You may use the myAppend predicate. For example:
?- myReverse([a,b,c,d,e],R).
R = [e,d,c,b,a]
11. myLast/2, which succeeds if the second argument is the last element of the list in the
first argument. For example:
?- myLast([a,b,c,d],d).
true
12. mySelect/3, where the first argument is an element of a list and the second
argument is a list. The third argument is matched with the list after the element has
been removed, if it exists. For example:
?- mySelect(c,[a,b,c,d],R).
R = [a,b,d]
Note: The built-in predicates for problems 7-12 are: length/2, member/2, append/3,
reverse/2, last/2, and select/3.

13. dup/2, where the first argument is a list and the second argument is matched with the
list after each element of the list has been duplicated. For example:
?- dup([a,b,c],R).
R = [a,a,b,b,c,c]
14. elemAt/3, where the first argument is a list and the second argument is an integer 𝑁𝑁.
The third argument is matched with the 𝑁𝑁th element of the list, if it exists. For example:
?- elemAt([a,b,c,d,e],4,R).
R = d
?- elemAt([a,b,c,d,e],7,R).
false
15. replace/4, where the first argument is a list and the second argument is an element
of the list. The fourth argument is matched with the list after each element of the list
has been replaced by the third argument. For example:
?- replace([a,b,c,a,b,c,a,b,c,a],c,p,R).
R = [a,b,p,a,b,p,a,b,p,a]
CSC330 W25 Programming Languages – Homework §2.5 Page 3/3

16. sumList/2, where the first argument is a list of numbers, and the second argument is
matched with the sum of all the numbers in the list. For example:
?- sumList([1,2,3,4],R).
R = 10
17. minimum/2, where the first argument is a list of numbers, and the second argument is
matched with the smallest number in the list. You may use the built-in operator min/2.
For example:
?- minimum([4,6,8,3,5,7],R).
R = 3
18. fill/3, where the first two arguments are integers, and the third argument is
matched with the list of all the integers between and including the given integers, if any.
Otherwise, the result is an empty list. For example:
?- fill(3,7,R).
R = [3,4,5,6,7]
19. sortedMerge/3, where the first two arguments are lists of sorted numbers. The third
argument is matched with the lists combined and sorted into one list. You may use the
built-in predicate append. For example:
?- sortedMerge([2,3,5,7],[2,4,8,16],R).
R = [2,2,3,4,5,7,8,16]
20. linearSearch/3, where the first argument is a list of integers and the second
argument is an integer 𝑥𝑥. The third argument is matched with the first position/index
where 𝑥𝑥 is found, otherwise it is matched with -1 if 𝑥𝑥 is not found in the list. (Assume
the index of the first position is 1.) For example:
?- linearSearch([2,3,5,7],7,R).
R = 4
?- linearSearch([2,3,5,7],6,R).
R = -1

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy