Tutorial Flashcards

1
Q

which are pairs and mpairs

A

it’s a iterator which provides the element and an index number (immutable and mutable respectively)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

items and mitems

A

it’s a iterator which provides immutable and mutable elements respectively

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Control flow statements scope

A

is not accessible from outside, is a new scope

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is block?

A

The block statement can be used to open a new block explicitly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

A block can be left prematurely with a

A

break statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what’s the purpouse Continue statement

A

Like in many other programming languages, a continue statement starts the next iteration immediately

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The when statement is almost identical to the if statement, but with these differences:

A

Each condition must be a constant expression since it is evaluated by the compiler.
The statements within a branch do not open a new scope.
The compiler checks the semantics and produces code only for the statements that belong to the first condition that evaluates to true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the utility of when statement

A

The when statement is useful for writing platform specific code, similar to the #ifdef construct in the C programming language.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to define a function

A
the concept of a procedure is needed. 
proc name(arg: type): typeOfReturn =
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

whait is the Result variable

A

A procedure that returns a value has an implicit result variable declared that represents the return value.
A return statement with no expression is a shorthand for return result.
The result value is always returned automatically at the end of a procedure if there is no return statement at the exit.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to assign params by reference

A
a var parameter can be used
proc divmod(a, b: int; res, remainder: var int) =
  res = a div b        # integer division
  remainder = a mod b  # integer modulo operation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the discard statement on the procedures definition

A

To call a procedure that returns a value just for its side effects and ignoring its return value, a discard statement must be used.
The return value can be ignored implicitly if the called proc/iterator has been declared with the discardable pragma

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the {.discardable.}

A

The return value can be ignored implicitly if the called proc/iterator has been declared with the discardable pragma

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to define a new operator

A

enclose the operator in backticks “``”:

For example
proc $ (x: myDataType): string = …
# now the $ operator also works with myDataType, overloading resolution

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to do a Forward declarations

A

The syntax for such a forward declaration is simple: just omit the = and the procedure’s body.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Iterators look very similar to procedures, but there are several important differences

A

Iterators can only be called from ‘for’ loops.
Iterators cannot contain a return statement (and procs cannot contain a yield statement).
Iterators have no implicit result variable.
Iterators do not support recursion.
Iterators cannot be forward declared, because the compiler must be able to inline an iterator. (This restriction will be gone in a future version of the compiler.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

what is the built-in $

A

the built-in $ (stringify) operator turns any basic type into a string, which you can then print to the console using the echo proc.

18
Q

what is a enum and its features

A

A variable of an enumeration type can only be assigned one of the enumeration’s specified values.
These values are a set of ordered symbols.
Each symbol is mapped to an integer value internally. The first symbol is represented at runtime by 0, the second by 1 and so on.

19
Q

what is the ‘type’ statement

A

the statetement to define a advanced type. As the var statement.

20
Q

how can you to convert any enumeration value to its name

A

with the $ operator

21
Q

how can you can convert any enumeration it to its underlying integer value.

A

with the ‘orc’ proc

22
Q

The symbols of enum types can be assigned an explicit ordinal value. How to do that.

A

he ordinal values must be in ascending order.

A symbol whose ordinal value is not explicitly given is assigned the value of the previous symbol + 1.

23
Q

what are the Ordinal types

A

Enumerations without holes, integer types, char and bool (and subranges)

24
Q

what are the Ordinal types features

A

ord(x) returns the integer value that is used to represent x’s value
inc(x) increments x by one
inc(x, n) increments x by n; n is an integer
dec(x) decrements x by one
dec(x, n) decrements x by n; n is an integer
succ(x) returns the successor of x
succ(x, n) returns the n’th successor of x
pred(x) returns the predecessor of x
pred(x, n) returns the n’th predecessor of x

25
Q

what are Subranges

A

A subrange type is a range of values from an integer or enumeration type (the base type). Example:

type
MySubrange = range[0..5]

26
Q

For what is the Natural type made for?

A

Other programming languages may suggest the use of unsigned integers for natural numbers. This is often unwise: you don’t want unsigned arithmetic (which wraps around) just because the numbers cannot be negative. Nim’s Natural type helps to avoid this common programming error.

27
Q

what are Sets

A

The set type models the mathematical notion of a set.

Sets can be constructed via the set constructor: {} is the empty set.

28
Q

Sets features

A

A + B union of two sets
A * B intersection of two sets
A - B difference of two sets (A without B’s elements)
A == B set equality
A <= B subset relation (A is subset of B or equal to B)
A < B strong subset relation (A is a real subset of B)
e in A set membership (A contains element e)
e notin A A does not contain element e
contains(A, e) A contains element e
card(A) the cardinality of A (number of elements in A)
incl(A, elem) same as A = A + {elem}
excl(A, elem) same as A = A - {elem}

29
Q

what are Sequences

A

Sequences are similar to arrays but of dynamic length which may change during runtime (like strings). Since sequences are resizable they are always allocated on the heap and garbage collected.

30
Q

what are Arrays

A

An array is a simple fixed length container. Each element in an array has the same type. The array’s index type can be any ordinal type.

31
Q

How to create a Sequence

A

x: seq[int]

x = @[1, 2, 3, 4, 5, 6]

32
Q

What are Open arrays

A

Note: Openarrays can only be used for parameters.
Often fixed size arrays turn out to be too inflexible; procedures should be able to deal with arrays of different sizes. The openarray type allows this.

33
Q

what are Varargs

A

A varargs parameter is like an openarray parameter. However, it is also a means to implement passing a variable number of arguments to a procedure. The compiler converts the list of arguments to an array automatically
This transformation is only done if the varargs parameter is the last parameter in the procedure header. It is also possible to perform type conversions in this context:

34
Q

what are Slices

A

Slices look similar to subranges types in syntax but are used in a different context. A slice is just an object of type Slice which contains two bounds, a and b.

35
Q

what are Tuples

A
A tuple type defines various named fields and an order of the fields. 
The constructor () can be used to construct tuples. 
The order of the fields in the constructor must match the order in the tuple's definition.
36
Q

what are Reference and pointer types

A

References (similar to pointers in other programming languages) are a way to introduce many-to-one relationships.
This means different references can point to and modify the same location in memory.

37
Q

what are Untraced references

A

Untraced references are also called pointers. Thus untraced references are unsafe. However for certain low-level operations (e.g., accessing the hardware), untraced references are necessary.
untraced references point to manually allocated objects or to objects elsewhere in memory.
untraced references are declared with the ptr keyword.

38
Q

what are Traced references

A

Traced references point to objects in a garbage collected heap,
Traced references are declared with the ref keyword;

39
Q

how to construct a Tuple

A

person = (name: “Peter”, age: 30)

var teacher: tuple[name: string, age: int] = (“Mark”, 42)

40
Q

how to import a module

A

import
import … except
from