The following repository contains solutions to problem sheets written in Racket for the module Theory of Algorithms. The module is taught to undergraduate students at GMIT in the Department of Computer Science and Applied Physics. The lecturer is Ian McLoughlin.
To clone this repository and run the solutions, do the following:
In the command line change to a directory:
cd <directory>
Clone the repository:
git clone https://github.com/DillonWard/Racket-Practical-Labs.git
Open and run the solutions using DrRacket
After you've installed DrRacket (see above) open the application.
Once the application is open, in the tool bar select the File
option and choose Open
so that you can select a solution.
Once you select Open
, open the directory where you cloned the solutions and select a .rkt
file to run. These files should have a small Racket logo to help identify them.
Finally, after you've opened your solution, select the RUN
option in the toolbar. This will run the solution and give the expected output.
Functional programming is the process of building software by composing pure functions, avoiding shared state, mutable data, and side-effects. Functional programming is declarative rather than imperative, and application state flows through pure functions. Contrast with object oriented programming, where application state is usually shared and colocated with methods in objects.
Functional programming is a programming paradigm, meaning that it is a way of thinking about software construction based on some fundamental, defining principles. Other examples of programming paradigms include object oriented programming and procedural programming.
Racket is a general purpose, multi-paradigm programming language in the Lisp-Scheme family. One of its design goals is to serve as a platform for language creation, design, and implementation. The language is used in a variety of contexts such as scripting, general-purpose programming, computer science education, and research.
Scheme is a programming language that supports multiple paradigms, including functional programming and imperative programming, and is one of the two main dialects of Lisp. Unlike Common Lisp, the other main dialect, Scheme follows a minimalist design philosophy specifying a small standard core with powerful tools for language extension.
Note: Some solutions don't use variable names given in problem sheet, out of preference and convenience for myself.
CA 2018 Theory of Algorithms The following exercises should be completed in the Racket programming language. Remember to plan your work and make regular commits to your repository. The instructions for submitting your work are given on the Moodle page. Note that “from scratch” means using only cons, car, cdr, define, lambda, if, null, null?, cond, map, = and the basic numerical operators (+, -, *, /, modulo). Other basic functions may be allowed, but please confirm their use with the lecturer.
Write, from scratch, a function in Racket that uses a brute-force algorithm that takes a single positive integer and return true if the number is a prime and false otherwise.
Call the function decide-prime
.
Write, from scratch, a function in Racket that takes a positive integer n0 as input and returns a list by recursively applying the following operation, starting with the input number.
End the recursion when (or if) the number becomes 1. Call the function collatz-list
.
So, collatz-list
should return a list whose first element is n0, the second element
is n1, and so on.
> (collatz-list 5)
'(5 16 8 4 2 1)
Write, from scratch, two functions in Racket. The first is called lcycle
. It takes a
list as input and returns the list cyclically shifted one place to the left. The second
is called rcycle
, and it shifts the list cyclically shifted one place to the right.
> (lcycle (list 1 2 3 4 5))
'(2 3 4 5 1)
> (rcycle (list 1 2 3 4 5))
'(5 1 2 3 4)
Write a function sublsum
in Racket that takes a list (of integers) as input and returns
a list of sublists of it that sum to zero. For this problem, you can use the
combinations
built-in function. Note the order of the sublists and their elements
doesn’t matter.
Write a function hamming-weight
in Racket that takes a list l as input and returns
the number of non-zero elements in it.
> (hamming-weight (list 1 0 1 0 1 1 1 0))
5
Write a function hamming-distance
in Racket that takes two lists and returns the
number of positions in which they differ.
> (hamming-distance (list 1 0 1 0 1 1 1 0) (list 1 1 1 1 0 0 0 0))
5
Write a function maj
in Racket that takes three lists x, y and z of equal length and
containing only 0’s and 1’s. It should return a list containing a 1 where two or more
of x, y and z contain 1’s, and 0 otherwise.
> (maj (list 0 0 0 0 1 1 1 1) (list 0 0 1 1 0 0 1 1) (list 0 1 0 1 0 1 0 1))
'(0 0 0 1 0 1 1 1)
Write a function chse
in Racket that takes three lists x, y and z of equal length and
containing only 0’s and 1’s. It should return a list containing the elements of y in
the positions where x is 1 and the elements of z otherwise.
> (chse (list 0 0 0 0 1 1 1 1) (list 0 0 1 1 0 0 1 1) (list 0 1 0 1 0 1 0 1))
'(0 1 0 1 0 0 1 1)
Write a function sod2
in Racket that takes three lists x, y and z of equal length and
containing only 0’s and 1’s. It should return a list containing a 1 where the number of
1’s in a given position in x, y and z contains an odd nubmer of 1’s, and 0 otherwise.
> (sod2 (list 0 0 0 0 1 1 1 1) (list 0 0 1 1 0 0 1 1) (list 0 1 0 1 0 1 0 1))
'(0 1 1 0 1 0 0 1)
Write a function lstq in Racket that takes as arguments two lists l and m of equal length and containing numbers. It should return d, the distance given by the sum of the square residuals between the numbers in the lists:
This means take the i element of m from the i element of l and square the result for all i. Then add all of those to get d.
> (lstq (list 4.5 5.1 6.2 7.8) (list 1.1 -0.1 6.1 3.8))
54.61