logo

Crowdly

Browser

Add to Chrome

Programming Styles 2025

Looking for Programming Styles 2025 test answers and solutions? Browse our comprehensive collection of verified answers for Programming Styles 2025 at www.icorsi.ch.

Get instant access to accurate answers and detailed explanations for your course questions. Our community-driven platform helps students succeed!

Conditional computation

For something less strange, let's see how we can take decisions in Haskell. You're certainly familiar with if statements in your favorite programming language. But Haskell has no statements whatsoever.

You may also know that most programming languages offer a conditional expression construct. In C/Java/JavaScript, this construct is immediately recognizable by the unusual tokens ? and ::

<cond-expr> ? <then-expr> : <else-expr>

Python has the same construct, but with a different syntax that uses named keywords instead of punctuation:

<then-expr> if <cond-expr> else <else-expr>

Haskell's syntax is similar to Python in that it too uses named keywords, but in a more "linear" order, similar to what you expect from Java/JavaScript:

if <cond-expr> then <then-expr> else <else-expr>

For example, we could write:

ghci> if True then "yay" else "nay"

"yay"

Given that this is an expression, we can use it to compose larger expressions:

ghci> "result: " ++ if True then "yay" else "nay"

"result: yay"

(If you feel more comfortable adding explicit parentheses around the whole if expression, please do so in the future.)

To wrap up this introduction, let's combine what we have learned. Define a function named description with one boolean parameter, status, which produces a "result" string as shown above. It should work like this:

ghci> description True

"result: yay"

ghci> description False

"result: nay"

Write the definition of the function:

View this question
Binding

You might have been surprised that Haskell is using = as a syntax for defining functions. Isn't = commonly used for assignments?

There are no assignments in Haskell: the proper name for the operation accomplished with = is binding. To bind means something like "tightly associate two things together".

What are we associating? Well, a name with an expression. For example:

ghci> favNumber = 42

ghci> twiceFavNumber = favNumber * 2

ghci> twiceFavNumber

84

Each = establishes an equation in the mathematical sense: twiceFavNumber is always replaceable with the expression favNumber * 2, and vice-versa favNumber * 2 is always replaceable with twiceFavNumber.

Defining functions is no different! We're binding the name of the function to its body. Whenever we see that name in the middle of the program, we can substitute it with the body of the function (replacing the parameters​ with the actual arguments​). This way of reasoning about programs is known as equational reasoning.

To stretch this concept, define a parameterless function named loop that simply calls itself. Then, try to call it and see what happens.

Write the full definition of the loop function:

View this question
Defining functions

We have seen how to call functions. Now we will tackle the flip side: how to define a function.

What are the ingredients for defining a function we already know from other programming languages? We need at least:

  • a name for the function
  • names for parameters, if any
  • the body of the function (defining the implementation)

For programming languages oriented towards the "imperative" paradigm, the body of the function is a sequence of statements (usually, one per line, possibly terminated by semicolons). For functional programming languages, the body is just an expression.

Compare this Python function:

def yell(name):

return name + "!"

and this definition in Haskell:

yell name = name ++ "!"

After defining it, we can use it like we did before with other functions:

ghci> yell "Luca"

"Luca!"

Despite the conciseness and the lack of explicit types in the definition, every function in Haskell has a statically known type! We were able to omit it because Haskell's type inference mechanism, which tries to figure out the types when they are left implicit.

Indeed, we can ask for the type of the yell function in the REPL:

ghci> :t yell

yell :: [Char] -> [Char]

Sure enough, the function yell takes in a String (list of characters) and produces a list of characters.

Go ahead and define a function isUSI with a parameter named univName which returns True iff the provided university name is "USI". You can use the familiar == operator to compare two expressions. Try to call it with a couple of different arguments to verify that it works as intended:

ghci> isUSI "ETH"

False

ghci> isUSI "USI"

True

Write the definition of the isUSI function:

View this question
Types

Haskell is a statically typed language, which means (among other things) that the compiler knows statically — before and without the need to execute a piece of source code — what its type is.

In the REPL, you can obtain this information with the special :t meta-instruction (instructions starting with a colon and a letter are typically instructions directed towards the REPL and only valid there).

For example, we can ask: what is the type of the literal True?

ghci> :t True

True :: Bool

The REPL responds that the expression True has type (::) Bool

.

(In general, Haskell places types "after". This is similar to Python, Scala, TypeScript, and many other languages; but different than C and Java, in which types generally are placed "before", for example in the signature of functions.)

A character literal is written within single quotes. What is the type of 'a'?

(Just answer with the type, not the full REPL output.)

View this question
Strings

String literals are enclosed within double quotes. What is the type of the expression "P" ++ "S" ++ "25" we evaluated earlier?

We don't need to compute the result and ask for its type. Go ahead and ask the REPL for the type of the whole expression:

View this question
Associativity

We'd like to write slightly bigger programs. We probably want to nest function calls. How can we do that, with Haskell's unusual syntax?

As a silly example, let's try to negate a boolean twice using the not function. Think about how you would do that.

You may have thought about doing it like this:

not not True

That doesn't work, though, because "function application" associates to the left. What does it mean? The expression we wrote above is equivalent to:

(not not) True

This cannot possibly work, because the first not expects an argument of Bool (and instead it received a function), and the second not doesn't have any argument.

Use the parentheses appropriately to write the call of not twice on True. Use the REPL to experiment.

View this question
Calling functions

Calling functions is the bread and butter of a programmer's life. Functional programmers prefer to say that they are applying a function on some argument(s). It is the exact same concept.

Haskell's syntax for calling (applying) functions differs from what you are used to in other languages, however. There are no parentheses. The name of the function and all the arguments are simply separated by spaces.

Here is an example, in which we call the function not on the argument True:

ghci> not True

False

We can also check the type of the function not:

ghci> :t not

not :: Bool -> Bool

It takes in a Bool and produces a Bool.

Write the call to the function max to determine whether 42 or 100 is the bigger number:

View this question
REPL

Start an interactive session of Haskell by running ghci.

A REPL (read-eval-print-loop) appears: the environment expects you to enter Haskell source code, which is read and evaluated to be able to print the result; all of this happens continuously in a loop.

We can enter the simple atomic expression 42, and the result is trivially going to be:

ghci> 42

42

We can try a slightly more complex expression, using the boolean literals​ True and False (note the first letter, which is capitalized) and the logic operators && (and) and || (or):

ghci> True && False

False

What is the result of evaluating the expression "P" ++ "S" ++ "25"?

View this question

Install Node.js (JavaScript Virtual Machine and Runtime)
 

We use Node.js as the JavaScript environment in this course. Node is based on Google's V8 JavaScript virtual machine.

Install node. You're free to install it in whatever way you want.

If you have other contexts in which you need possibly different versions of node, you may want to use nvm (Node Version Manager) to install and manage multiple different versions of node on your machine. You can install nvm e.g., by using Homebrew. Of course then you first have to install Homebrew. (If you develop software on a Mac, chances are high you already installed Homebrew.)

Which version of node are you running?

Run node --version to find out, and copy its output here:

View this question

Install JDK (Java Virtual Machine and Compiler)

 

There now is the commercial and the open version of JDK. We recommend to get the open version (called Open

JDK).

We will use Java 21 or later in this course. You can find the Eclipse Temurin distribution at adoptium.net.

What is the output of java --version after installing the JDK?

View this question

Want instant access to all verified answers on www.icorsi.ch?

Get Unlimited Answers To Exam Questions - Install Crowdly Extension Now!

Browser

Add to Chrome