Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens with a specific function name 'it'?

Tags:

haskell

ghci

I was just wrestling with a Haskell error for an hour, and then traced it to the use of a specific function name it. Can someone explain why function names fu and it behave differently in the transcript below? Specifically, why does the type of it change when it is called?

λ> fu x = x + 1
λ> :t fu
fu :: Num a => a -> a
λ> fu 1
2
λ> :t fu
fu :: Num a => a -> a
λ> it x = x + 1
λ> :t it
it :: Num a => a -> a
λ> it 1
2
λ> :t it
it :: Num a => a

When ghci is started fresh, it reports that it is not in scope, so I do not think it should have any special meaning by default.

like image 202
jhu Avatar asked Oct 25 '20 12:10

jhu


People also ask

What happens when a function is called?

Now, whenever a function is called a new stack frame is created with all the function's data and this stack frame is pushed in the program stack, and the stack pointer that always points the top of the program stack points the stack frame pushed as it is on the top of the program stack.

What happens when a function is called in Python?

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.

What is a function name?

Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature. Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument.

When a function is called with in its own function it is known as?

A function that calls itself is, as you suspect, called "recursive".

Why should I name my functions and variables?

You will not just write abetter and more readable code, but also make the code easier to understand for the person trying to understand it or even for your future self. When you are naming a function, variable or a class, the same principles apply. You should think of a name as a tiny comment you put in your code.

How do you define a function in Python?

The general syntax for creating a function in Python looks something like this: Let's break down what's happening here: def is a keyword that tells Python a new function is being defined. Next comes a valid function name of your choosing. Valid names start with a letter or underscore but can include numbers.

What is the best way to name a function?

When you are naming a function, variable or a class, the same principles apply. You should think of a name as a tiny comment you put in your code. The key idea when naming something is to convey as much information as possible. Pack information into names.

What is a function in C++?

A function is a set of code that performs a specific task and can be used whenever needed just by calling it. While using multiple function calls or recursion, the concept of a function call is very necessary to be known, for better understanding of the code.


Video Answer


1 Answers

In GHCi, it is always bound to the last evaluated expression, and shadows its previous value:

Prelude> 2 + 3
5
Prelude> it
5
Prelude> 3 * 4
12
Prelude> it
12

In the beginning of the GHCi session, it's not defined.

like image 132
bereal Avatar answered Oct 17 '22 13:10

bereal