Assignment 1 Exploring Programming Language Concepts Solution

$30.00 $26.40


  1. Provide a common definition of functional programming. (2 marks)

  1. Haskell is considered by many to be a pure functional programming language. Explain the following piece of Haskell code and discuss its relationship with the definition in Q. 1.

lu2153c5e5nd tmp e84540ab07f74ee7

import Data.IORef

main :: IO ()

main = do

putStrLn “I’m going to calculate a sum, hang on a sec”

totalRef <- newIORef (0 :: Int)

let loop i

| i > 100 = pure ()

| otherwise = do

oldTotal <- readIORef totalRef

let newTotal = oldTotal + i

writeIORef totalRef $! newTotal

loop $! i + 1

loop 1

total <- readIORef totalRef

putStrLn $ “The total is ” ++ show total

  1. Immutability is preferable over mutability”. Explain why this is normally considered correct.

  1. Consider the following (pseudo-) machine code:

lu2153c5e5nd tmp 6236e49359fe8ba6

mov R1, $y

mov R2, $z

add R3, R1, R2

mov $x, R3

    1. Write the equivalent code in C.

    1. Write the equivalent code in Haskell.

    1. Since this code is mutable, what does it imply for ALL languages?

  1. Consider the following code in F#:

lu2153c5e5nd tmp 46c03471223a459d

let sqrtx x = x * x

let imperativefun list =

let mutable total = 0

for i in list do

let x = sqrtx i

total <- total + x


let functionalfun list =


|> sqrtx

|> Seq.sum

  1. What does each function do in the previous code?


lu2153c5e5nd tmp 4f0356d442ccc758 ECE 421 | Exploring Software Development Domains

  1. Consider a subset of ISO 9126





Argue about the impact, if any, of the two different implementations (imperativefun and functionalfun) on these characteristics.

    1. Utilize the sqrtx function in Q5 to write a function which raises its argument to the 4th power.

  1. Pure functions: A pure function is a function that, given the same input, will always return the same output and does not have any observable side effect. Functional programming likes pure functions; which of the following are pure functions:

changing the file system

inserting a record into a database

making an http call


printing to the screen / logging

obtaining user input

querying the DOM

accessing system state


  1. Based on the definition of functionalfun presented in Q5, write a function in Rust that takes a number

    1. and returns =1 2 + 2.

  1. Write a Rust function that computes the volume of a sphere, given its radius.

  1. What does the following Scheme function do?

lu2153c5e5nd tmp d045bb53ef1d94dd

(define (x lis)


((null? lis) 0)

((not (list? (car lis)))


((eq? (car lis) #f) (x (cdr lis)))

(else (+ 1 (x (cdr lis))))))

(else (+ (x (car lis)) (x (cdr lis))))

  1. Total functions state that, for every valid input value, there is a valid, terminating output value. In contrast to a total function, a partial function may result in an infinite loop, program crash, or runtime exception for some input.

a. Explain what happens when you present the following Haskell code to its compiler

lu2153c5e5nd tmp d532afd470043ecd

data Colour = Red | Yellow | Blue

sayColour colour =

case colour of

Red -> “red”


lu2153c5e5nd tmp 4f0356d442ccc758 ECE 421 | Exploring Software Development Domains

Yellow -> “yellow”

lu2153c5e5nd tmp 7c6dea63b37d51ed

main = putStrLn (sayColour Blue)

b. Explain what happens when you present the following Rust code to its compiler

lu2153c5e5nd tmp 3166a79d9cb842e8

enum Colour {





fn say_colour(colour: &Colour) -> &’static str {

match colour {

Colour::Red => “red”,

Colour::Yellow => “yellow”,



fn main() {

println!(“{}”, say_colour(&Colour::Blue));