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 IDEs
 

You are free to use any editor or IDE in this course.

You could use Visual Studio Code for everything (via extensions it supports all the major languages of this course: JavaScript, Java, Python, and Haskell). You can download the latest release from Microsoft.

If you use Visual Studio Code for Java Development, install the "Coding Pack for Java". Read more about Java development with VS Code on the VS Code web site.

You may also prefer more specialized IDEs, for example IntelliJ IDEA for developing Java code. You can download the latest release of IntelliJ IDEA from JetBrains.

When you create new Java projects in IntelliJ, you may then want to configure your project settings so the project uses your JDK:

Menu: File | Project Structure... | Project SDK

Pick the version you installed if it's already listed. Otherwise download and add a new version.

Which IDEs are you going to use?

View this question
Install GHC (Glasgow Haskell Compiler)

We use GHC as the Haskell compiler and interactive environment for Haskell. GHC is an open-source project and its earliest prototype was created at the University of Glasgow.

Install GHC. We recommend the automatic official installation script you can run with the command shown on this page. All the default choices are fine for our purposes. (Installing it via Homebrew is also possible, but less recommended.) The installation of the compiler and the related tools may take a while.

Which version of GHC are you running?

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

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