i'm pretty new in programming and learning Standard ML at the moment at my university in Germany. My question is:
What are abstractions and why am I using it? And moreover: why can I call them up by just using "val it=..."?
For example:
fn (x:int) => x*x:
it 6;
val it = 36;
And another example:
(fn (x:int)=>x*x) 7
val it = 49;
Why is it working that way? My Tutors weren't able to give me an accurate answer, so i thought you guys could lend me your knowledge.
So thank you in anticipation!
You ask two questions:
What are abstractions and why am I using [them]?
Abstraction is not a feature specific to Standard ML. It is a technique for managing complexity. See the Wikipedia article on Abstraction in Computer Science. Objects from object-oriented programming are one abstraction to manage complexity, but in Standard ML you will mostly use functions to create abstraction.
I.e. whenever a function does a lot of things, it does not have to look complicated, because it can call upon smaller functions that perform parts of the computation in isolation. Each small part easily makes sense in their limited scope of responsibility.
Why is
(fn (x:int)=>x*x) 7
working that way?
I am unsure exactly what you are referring to here. Whenever you write an expression into the interactive interpreter, rather than a declaration such as val x = 42;
or val f = fn x => x*x;
, the interactive interpreter assumes an implicit val it = ...whatever you typed...
.
In this case, it is an anonymous function that squares its input applied to the integer 7. Whether it is temporarily named it
or not should not affect the outcome. This is not exactly an example of abstraction over smaller parts of a Standard ML program.