Is there a way to define a constant in SML in a let binding way.
So basically what I'm asking is how to for example do constant x = 5, in the way below:
let
....
in
...
end
Not sure I understand the question correctly, but
let val x = 5 in ... end
sounds like it did what you're asking for.
Related
Why this Fortran code is incorrect?
function foo(x)
real x
real, dimension(3) :: foo
foo = (/1, 2, 3/)
end
... and in main program
print*, foo(x)(1)
Why we cannot access element in function result directly?
While you ponder your own question
Why we cannot access element in function result directly?
I suggest you also write lines, in your main program, such as
res = foo(x) ! having taken care to declare res appropriately
print*, res(1)
and get on with your coding. It's just not syntactically-correct to index a function call the way you've tried.
So one answer to your original question is because that's the way Fortran's syntax is defined to which you might be prompted to respond why is Fortran's syntax defined that way ? Even if this process turns up an answer in the form of reference to the roots of the design of Fortran (now over 50 years old) you're still going to have to modify your code to align with Fortran's syntax. For sure your compiler isn't going to say you know, what you've written is better than the syntax I've been programmed to accept, I'll compile that up right now ...
The answer by High Performance Mark tells all needed. As you're after syntactic niceness I'll address one thing in there: "having taken care to declare res appropriately".
One could use an associate construct to hide this a little.
associate (res => foo(x))
print *, res(1)
end associate
This changes nothing in that answer other than reducing junk declarations.
I'm new with the scilab synthax, and I want to know if there is a way to
declare a poly, like p = 3x + 2, and use something like p(5) to get 17 as an answer. The reason to do so is that the poly synthax is a lot easier than defining the same expression over and over again.
In scilab polynom can be define with special function poly. In your case, this command will look like this:
x=poly(0,"x");
p = 3*x+2
for obtain value in point need use function horner:
horner(p,5)
and we get
ans =
17.
I made a simple recursive function, and expected it to work (but it doesn't):
open System
open System.Threading
let f =
let r = Random()
let rec d =
printfn "%d" (r.Next())
Thread.Sleep(1000)
d
d
f
With the help of Intellisense I ended up with the following working function (but without understanding why previous function didn't work):
open System
open System.Threading
let f : unit =
let r = Random()
let rec d() =
printfn "%d" (r.Next())
Thread.Sleep(1000)
d()
d()
f
So why do I need to explicitly state unit and ()?
In the first version, you declared a recursive object (let rec d), or a value. You're saying that the d object is recursive, but how an object could be recursive? How does it call itself? Of course, this doesn't make sense.
It's not possible to use recursive objects in F# and this is the reason why your first version doesn't work.
In the second version, you declared a recursive function (let rec d()). Adding (), you're explicitly stating that d is a function.
Furthermore you explicitly stated, with unit, that the function f (called just once) will not return anything, or, at least, you're saying that f will return a value of a not specific type. In F#, even the simplest functions must always return a value.
In your case, F# will try to infer the type that f will return. Because there's no specific type annotation and your f is not doing something (like a calculation) that will return a specific value using a specific type, the F# compiler will assign a generic return type to f, but your code is still ambiguous and you have to specify the unit type (the simplest type that a F# function could return) to be more specific.
The value restriction error is related indeed to F#'s powerful type inference. Please have a look at this interesting article about this error.
In your first attempt, you define not a function, but a value. The value d is defined in terms of itself - that is, in order to know what d is, you need to first know what d is. No wonder it doesn't work!
To make this a bit more clear, I will point out that your definition is of the same kind as this:
let x = x
Would you expect this to work?
In your second attempt, you gave d a parameter. It is the parameter that made it a function and not a value. Compare:
let rec x() = x()
This will still cause a stack overflow when executed, but at least it will compile: it's a function that unconditionally calls itself.
You didn't have to give it specifically a unit parameter, any parameter would do. You could have made it a number, a string, or even a generic type. It's just that unit is the simplest option when you don't care what it is.
And you didn't actually need to annotate f with a type. That was an extraneous step.
In conclusion, I'd like to point out that even in your second code block, f is still a value, not a function. In practical terms it means that the code inside f will be executed just once, when f is defined, and not every time you mention f as part of some other expression, which is apparently what you intuitively expect.
I realize this is a really simple bit of code, and I'm quite sure it's recursive, but I just want to make sure it is what I think it is. (Sorry if this is kind of a lame question, I'm just second guessing myself on if I understand what recursion actually is.)
var x = 0
func countToTen() {
if (x <= 10) {
println(x)
x++
countToTen()
}
}
Yes, this is definitely recursive! For good style, however, it is best to make x a parameter to the function. It's sort of a style issue, but it also makes the code easier to maintain to not have global variables like you have here.
here's what I'm talking about.
func countToTen(x) {
if (x <= 10) {
println(x)
countToTen(x + 1)
}
}
Now you can just call the function
countToTen(1)
And that would count from 1 to ten, for instance. You did it correctly, my version is just perhaps slightly cleaner form.
In programming, if a method:
Calls itself and,
Moves towards a base case (in this case x == 10)
then it is recursive.
Here is a post about real-world examples.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why are functions in Ocaml/F# not recursive by default?
OCaml uses let to define a new function, or let rec to define a function that is recursive. Why does it need both of these - couldn't we just use let for everything?
For example, to define a non-recursive successor function and recursive factorial in OCaml (actually, in the OCaml interpreter) I might write
let succ n = n + 1;;
let rec fact n =
if n = 0 then 1 else n * fact (n-1);;
Whereas in Haskell (GHCI) I can write
let succ n = n + 1
let fact n =
if n == 0 then 1 else n * fact (n-1)
Why does OCaml distinguish between let and let rec? Is it a performance issue, or something more subtle?
Well, having both available instead of only one gives the programmer tighter control on the scope. With let x = e1 in e2, the binding is only present in e2's environment, while with let rec x = e1 in e2 the binding is present in both e1 and e2's environments.
(Edit: I want to emphasize that it is not a performance issue, that makes no difference at all.)
Here are two situations where having this non-recursive binding is useful:
shadowing an existing definition with a refinement that use the old binding. Something like: let f x = (let x = sanitize x in ...), where sanitize is a function that ensures the input has some desirable property (eg. it takes the norm of a possibly-non-normalized vector, etc.). This is very useful in some cases.
metaprogramming, for example macro writing. Imagine I want to define a macro SQUARE(foo) that desugars into let x = foo in x * x, for any expression foo. I need this binding to avoid code duplication in the output (I don't want SQUARE(factorial n) to compute factorial n twice). This is only hygienic if the let binding is not recursive, otherwise I couldn't write let x = 2 in SQUARE(x) and get a correct result.
So I claim it is very important indeed to have both the recursive and the non-recursive binding available. Now, the default behaviour of the let-binding is a matter of convention. You could say that let x = ... is recursive, and one must use let nonrec x = ... to get the non-recursive binder. Picking one default or the other is a matter of which programming style you want to favor and there are good reasons to make either choice. Haskell suffers¹ from the unavailability of this non-recursive mode, and OCaml has exactly the same defect at the type level : type foo = ... is recursive, and there is no non-recursive option available -- see this blog post.
¹: when Google Code Search was available, I used it to search in Haskell code for the pattern let x' = sanitize x in .... This is the usual workaround when non-recursive binding is not available, but it's less safe because you risk writing x instead of x' by mistake later on -- in some cases you want to have both available, so picking a different name can be voluntary. A good idiom would be to use a longer variable name for the first x, such as unsanitized_x. Anyway, just looking for x' literally (no other variable name) and x1 turned a lot of results. Erlang (and all language that try to make variable shadowing difficult: Coffeescript, etc.) has even worse problems of this kind.
That said, the choice of having Haskell bindings recursive by default (rather than non-recursive) certainly makes sense, as it is consistent with lazy evaluation by default, which makes it really easy to build recursive values -- while strict-by-default languages have more restrictions on which recursive definitions make sense.