Professional Mercury FAQ Questions and Answers

Welcome to our comprehensive Trivia Quiz and interview preparation guide. Below you will find a curated list of popular Trivia Questions and Answers specifically for Mercury. Whether you are preparing for a technical interview or just testing your knowledge, these FAQ Questions will help you succeed.

How do you debug Mercury programs?

Using mdb (Mercury Debugger) or tracing.

Why use Mercury over Prolog?

Mercury offers better performance, static typing, and explicit determinism handling.

What is Mercury?

Mercury is a logic-functional programming language designed for high-performance declarative programming.

Who developed Mercury?

Mercury was developed at the University of Melbourne by Zoltan Somogyi and his team.

What programming paradigms does Mercury support?

Mercury supports logic, functional, and declarative programming paradigms.

How is Mercury different from Prolog?

Mercury enforces strong static typing, determinism, and has better optimization compared to Prolog.

What is the primary goal of Mercury?

To provide a high-performance and reliable declarative programming language with strong typing and determinism.

What type of typing does Mercury use?

Mercury uses strong static typing.

What is a mode in Mercury?

A mode defines the input/output behavior of a predicate, specifying which arguments must be given and which are computed.

How does Mercury handle determinism?

Mercury uses determinism categories like det, semidet, multi, nondet, and erroneous to specify behavior.

How do you declare a predicate in Mercury?

Using :- pred followed by the predicate signature, e.g., :- pred factorial(int, int).

How do you implement a predicate in Mercury?

Using :- mode and :- pred, then defining it with :- implementation.

What is the purpose of :- mode in Mercury?

It specifies how arguments should be used (input/output).

What are the common modes in Mercury?

in, out, di (destructive input), and uo (unique output).

How does Mercury handle higher-order programming?

Mercury supports higher-order functions using function types and lambda expressions.

What is a functional type in Mercury?

A functional type allows defining functions, e.g., :- func square(int) = int.

How do you write a recursive function in Mercury?

Similar to other functional languages, using base and recursive cases.

What is determinism in Mercury?

It defines the number of valid solutions a predicate can have.

What determinism type ensures exactly one solution?

det (deterministic) ensures exactly one solution.

What determinism type allows zero or one solutions?

semidet (semi-deterministic) allows either zero or one solutions.

What determinism type allows multiple solutions?

multi (multi-deterministic) allows multiple solutions.

What determinism type allows failure?

nondet (non-deterministic) can succeed multiple times or fail.

How do you declare a list in Mercury?

Using list(T), e.g., list(int).

How do you construct a list in Mercury?

Using `[Head

How do you append two lists in Mercury?

Using list.append(List1, List2).

How do you perform pattern matching in Mercury?

Using `Head

How do you define a user-defined type in Mercury?

Using :- type, e.g., :- type color ---> red ; green ; blue.

What is a fact in Mercury?

A fact is a rule with no body, used to define static knowledge.

What is a rule in Mercury?

A rule is a clause defining a predicate with conditions.

How do you define a conditional statement in Mercury?

Using if-then-else, e.g., X = (if Y > 0 then "Positive" else "Negative").

How do you define a module in Mercury?

Using :- module ModuleName.

How do you import a module in Mercury?

Using :- import_module ModuleName.

What is the default execution mode in Mercury?

Mercury runs in a strict evaluation mode by default.

What is a lambda function in Mercury?

A function defined inline using func(X) = X * X.

How do you perform input/output in Mercury?

Using the io module, e.g., io.write_string("Hello", !IO).

How does Mercury handle side effects?

Using the io state variable, represented as !IO.

How do you read input in Mercury?

Using io.read_line_as_string(Result, !IO).

What is the difference between func and pred in Mercury?

func defines a function returning a value, while pred defines predicates with possible multiple solutions.

What is tail recursion in Mercury?

A recursive function where the recursive call is the last operation in the function.

How does Mercury optimize tail-recursive functions?

Mercury automatically transforms them into loops for better performance.

What is backtracking in Mercury?

The ability to explore multiple solutions in a non-deterministic manner.

How do you define an accumulator in Mercury?

Using an extra parameter to store computed values across recursive calls.

What is a unique mode in Mercury?

di (destructive input) and uo (unique output) enforce single-use references for safety.

What is DCG in Mercury?

Definite Clause Grammars (DCG) are used for parsing and grammar representation.

How do you implement DCG in Mercury?

Using --> notation in predicate definitions.

How do you compile a Mercury program?

Using mmc filename.m.

How do you run a Mercury program?

Using ./filename after compiling.

What are Mercury's backends?

Mercury supports multiple backends, including C, Java, and .NET.

How does Mercury integrate with C?

Using pragma foreign_code and pragma foreign_proc.

How do you handle errors in Mercury?

Using require predicates and assertions.