Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml: bound expressions v. functions

Here we have a function definition:

let f x = x + 3;;

Here is an expression:

let g = 4;;

Could g just be thought of as constant function that takes no arguments? Is there any difference?

like image 565
Nick Heiner Avatar asked Dec 23 '22 09:12

Nick Heiner


1 Answers

Yes - From a totally functional point of view (like practised in Haskell), everything is a function (Really everything).

And since a purely-functional language disallows any kind of change, this definition does not bear any contradictions.

Is there any difference?

Well, OCaml is not purely-functional. This means the functions are allowed to perform side-effects which differs a bit from the definition of a constant value.

This code (F# here - but quite similar in Caml) would be perfectly valid.

let name = 
    Console.Write("Enter your Name: ")
    Console.ReadLine()
like image 139
Dario Avatar answered Dec 25 '22 22:12

Dario