Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why these assignments produce different results?

Tags:

f#

Why

let ab a b = a 5 + b

will produce

val ab : a:(int -> int) -> b:int -> int

and

let ab2 a b = a 5 +b

will produce

val ab2 : a:(int -> int -> 'a) -> b:int -> 'a

Why this one space between '+' and 'b' makes this difference?

like image 452
Twelve Avatar asked Aug 20 '15 11:08

Twelve


People also ask

What makes an assignment effective?

The most effective and challenging assignments focus on questions that lead students to thinking and explaining, rather than simple yes or no answers, whether explicitly part of the assignment description or in the brainstorming heuristics (Gardner, 2005).

Why the assignments are important in learning process explain?

An assignment is a piece of (academic) work or task. It provides opportunity for students to learn, practice and demonstrate they have achieved the learning goals. It provides the evidence for the teacher that the students have achieved the goals.

How do grades on assignments impact your learning?

Additionally, grading provides students with feedback on their own learning, clarifying for them what they understand, what they don't understand, and where they can improve. Grading also provides feedback to instructors on their students' learning, information that can inform future teaching decisions.


1 Answers

It is all down to how the parser prioritises different syntactic options to avoid ambiguity.

+ is both the binary addition operator and the unary "positive"1 operator. 5 + b is thus the application of addition to two arguments; but +b is the positive operator applied to some symbol b.

Thus

let ab a b = a 5 + b

is parsed as:

let ab a b = (a 5) + b

with a being a function of one integer argument returning an int so it can be added to b; but

let ab2 a b = a 5 +b

is parsed as:

let ab2 a b = a (5) (+b)

with a being a function of two arguments, with no way to infer the type it returns.

1 I don't have an F# operator list to hand, so can't check the correct name. Edit: it appears I couldn't remember correctly: Arithmetic Operators (F#) :-).

like image 96
Richard Avatar answered Oct 16 '22 06:10

Richard