Howdy all!
I agree 100% with david. I would like to give a complement to his nice
remarks :)
In functional languages, there is an important different between
"mutable" and "immutable" variables.
mutable variables are variables that can be modified in place while
immutable variables cannot be modified (they are static)
Most of the usual languages, such as C, php, etc. have mutable
variables by default. For instance, the following code (which will not
work in liquidsoap):
-----
x = 2
if (some condition) then
x = 3
# This prints: x value is 3
print("x value is #{x}")
-----
In this code, x is mutable and its value is changed in-place inside
the conditional call.
On the contrary, immutable variables cannot be modified. In C, this
could be though as a const type declaration for instance.
In functional languages most variables are immutable. Thus, in order
to change the value of x, the above code would rewrite as:
-----
x = 2
x =
if (some condition) then
3
else
x
fi
# This prints x value is 3
print("x value is #{x}")
-----
In this case, since x cannot be modified in-place, its value is
changed by redefining a new x with the updated value.
In some cases, though, mutable variables can be used too, although
they should not be abused. In recent (1.0beta) liquidsoap, this is
acheived through variable references:
-----
x = ref 2
if (some condition) then
x := 3
fi
# This print x value is 3
print("x value is #{ !x }")
-----
In this case, the variable x is like a box, whose content is changed
using x := 3 and accessed using !x
Most of the time, you should not need to use variable references.
However, they become very handy in some cases, for instance in order
to record the latest metadata seen in a stream:
----
s = (some source)
latest_metadata = ref []
def save_latest(m) =
latest_metadata := m
end
s = on_metadata(save_latest,s)
------
In the code above, at any time after the first metadata is passed,
!latest_metadata will return the value of the latest metadata seen in
the stream, which can be useful for instance to define a telnet/server
command returning this value at any time..
Romain
2011/6/16 David Baelde <[email protected]>:
> Hi Martin,
>
> On Thu, Jun 16, 2011 at 1:09 PM, Martin Hamant <[email protected]> wrote:
>> Is it right to say, output.icecast() take a format and a source as args,
>> "returning" a new source ?
>> So what about the type (format, source) => source
>
> It is right to say that, and the type you wrote is correct. Precisely,
> we would write (format,source)->source (and in fact,
> (format('a),source('a))->source('a)) but not format->source->source
> (Romain wrote this as a shortcut, and because it is written like that
> in other languages).
>
>>> But if you do:
>>> out(%mp3)
>>> Then the variable out is still of type format -> source -> source
>>> because the above was not kept..
>>
>> can you define "not kept" (from ? why ?)
>
> The expression out(%mp3) is correct but you need to do something with
> it. In your case, you want to update the definition of out to become
> out(%mp3). So the new definition should not be inside the conditional.
> You can write this as follows (I write extra parenthesis to clarify):
>
> # initial def
> out = output.icecast(...)
> # new definition
> out = (if ... then out(%mp3) else ... end)
> # at this point out may actually be out(%mp3)
> # so it only expects a source now
>
> The important point is to write the if-then-else inside the second
> definition, and not the other way around. This may seem unusual, but
> the key is to realize that if-then-else is an expression constructor
> like another. This is unlike in language like C/Java/Python/etc where
> if-then-else only forms statements/instructions (something like x :=
> 1, print(x)) but not expressions (something like x+1, x<=3, etc). In
> fact, everything is an expression in liquidsoap (and more generally in
> functional languages).
>
> Hope this helps,
> --
> David
>
------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
Savonet-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/savonet-users