CS 331 Spring 2025 > Assignment 7 (Coding in Scheme & Prolog)
CS 331 Spring 2025
Assignment 7 (Coding in Scheme & Prolog)
Assignment 7 is due at 5 pm Thursday, April 24. (Note that, as per the syllabus, this assignment will not be accepted after the beginning of the final exam period: 1 pm Friday, May 2.) The assignment is worth 70 points.
Procedures
This assignment is to be done individually.
Turn in answers to the exercises below on the UA Canvas site, under Assignment 7 for this class.
- Your answers must consist of
the answer to Exercise A
(submitted as an attached text/PDF file),
the source code for Exercise B
(file
alternatingSums.scm
), and the source code for Exercise C (filecollcount.pl
). - I may not look at your homework submission immediately. If you have questions, e-mail me.
Exercises (A–C, 70 pts total)
There will be no test programs posted for this assignment. Test your code!
Exercise A — Running a Fulmar Program
Purpose
In this exercise you will demonstrate that you can execute Fulmar code.
Instructions
Get the file check_fulmar.fmar
from the class Git repository.
This is a Fulmar source file for a complete program.
When it is executed,
this program prints
“Secret message #5:
”.
Below that, it prints a secret message.
Run the program.
What is the secret message?
To execute the program, run fulmar.lua
,
with the following files in the same directory:
check_fulmar.fmar
,
from the Git repository,
and
lexit.lua
,
parseit.lua
,
and
interpit.lua
,
from previous assignments.
At the prompt, type:
:r check_fulmar.fmar
Exercise B — Programming in Scheme
Purpose
In this exercise, you will write a Scheme procedure.
Instructions
Write a Scheme source file alternatingsums.scm
as follows.
Be sure to follow the
Coding Standards.
- Your file must begin with the line
“
#lang scheme
”. - Your file must define procedure
alternatingSums
.- This takes an arbitrary number of arguments,
which will be numbers.
It returns a list of length 2 holding the sum of the even-index numbers
and the sum of the odd-index numbers.
Indices are considered to begin at zero.
For example evaluating
(alternatingSums 1 20 3 40 5)
returns(9 60)
, because \(1+3+5 = 9\), and \(20+40 = 60\). And(alternatingSums)
returns(0 0)
. - Your code does not need to do any type checking or other error checking.
- Here is a sample DrRacket session using
alternatingSums
. (Filealternatingsums.scm
has already have been opened and run.)[Interactive Scheme]
> (alternatingSums 1 2 3 4) (4 6) > (alternatingSums) (0 0) > (alternatingSums 56) (56 0) > (alternatingSums 10 20 30 40 50) (90 60) > (alternatingSums (* 2 2) (* 3 3) 1) (5 9)
- This takes an arbitrary number of arguments,
which will be numbers.
It returns a list of length 2 holding the sum of the even-index numbers
and the sum of the odd-index numbers.
Indices are considered to begin at zero.
For example evaluating
- Your file may define any other procedures, macros, or variables you wish.
Exercise C — Programming in Prolog
Purpose
In this exercise, you will write a Prolog predicate.
Instructions
Write a Prolog source file collcount.pl
as follows.
Be sure to follow the
Coding Standards.
- Your file defines the predicate
collcount/2
; that is,collcount
is a predicate that takes 2 arguments.- Predicate
collcount
takes arguments as follows:collcount(+n, ?c)
; that is, the first argument is an input, while the second is either an input or an output. - The idea is that
n
is a positive integer, andc
is the number of iterations of the Collatz function required to taken
to 1. - Your code does not need to do any type checking or other error checking.
- Here is a sample SWI-Prolog session using
collcount
.[Interactive Prolog]
?- [collcount]. true. ?- collcount(1, 0). true. ?- collcount(1, C). C = 0. ?- collcount(27, C). C = 111. ?- collcount(27, 200). false.
- Predicate
- Your file may define any other predicates you wish.
- You may find the following helpful.
Prolog has a numerical function
mod
than computes the integer division remainder, just like the “%
” operator in C++. For example:[Interactive Prolog]
?- K is mod(15, 4). K = 3.