Saturday, February 28, 2009
Friday, February 27, 2009
hdaemonize on hackage
Having got a green light from Andre, I uploaded hdaemonize to Hackage.
I also cleaned up the library a bit. Reading up on POSIX signals I decided that it is wrong for the daemonize library to mess with them beyond blocking HUP. Also, now it has two functions, daemonize :: IO () -> IO ()
which does the bare minimum, and serviced :: Program -> IO ()
that does that and more, for example writing a PID file, handling start/stop/restart, catching and logging exceptions to syslog.
Time to try it out in production.
R Language - once more
An early warning for the unwary hacker.
Normal-order evaluation
Y <- function (f)
(function (x) f(x(x))) (function (x) f(x(x)))
FAC <- function (f) function(n)
(if (n == 0) 1 else n * f(n - 1))
> Y(fac)(10)
[1] 3628000
Quoting and macros
fn <- function(x) {
x <- substitute(x);
f <- list(NULL, x[[2]]);
names(f) <- c(x[[3]], "");
as.function(f, envir=parent.frame());
}
y <- fn(f -> fn(x -> f(x(x)))(fn(x -> f(x(x)))))
fac <- fn(f -> fn(n -> if (n == 0) 1 else n * f(n - 1)))
> y(fac)(10)
[1] 3628800
Coercions
> NULL == FALSE
logical(0)
If you are wondering what happened here, FALSE, which is a logical vector of length 1, was compared for equality with NULL, which is an empty list. To proceed with the comparison, R coerced the list to a vector of length 0. Then, two logical vectors were compared elementwise for equality, to yield the resulting vector of length 0.
Equational reasoning is not a good starting point to make sense of this:
> NULL == NULL
logical(0)
> logical(0) == FALSE
logical(0)
Side effects
> f <- function () { print("MISSILES LAUNCHED"); 1 }
> g <- function (x <- f()) { x + 1 }
> g()
[1] "MISSILES LAUNCHED"
[1] 2
> g(1)
[1] 2
Argument parsing
> f <- function (x = y + 1, y = x + 1) { y <- 2; y }
> f()
[1] 2
Summary
Can a language safely combine the above features?
Does statistics really nead such a language?
References
- Lumley T. "Programmer's Niche: Macros in {R}", R News, 2001, Vol 1, No. 3, pp 11–13
- Sungwoo Park. A critique of R. Invited talk at Japanese R Users' Meeting, December 2006
- R Language Definition
Thursday, February 26, 2009
ANN: VIDEO SCREENING, A taste of Haskell (tutorial), Sat, Feb 28, 13:00-15:00, UEC, Kiev
LtU-Kiev mailing list
R Language
Recall
and call
. The latter is not unlike a (quote)
..
fac <- function (n) if (n == 0) { 1 } else { Recall(n-1) * n }; fac(10) eval(call(fac, 10))The downside is that the implementation is very slow. Since most operations vectorized, the argument is that it does not matter. Realistically, nobody is going to do anything about it. In terms of inter-op with libraries, would it not have been much better to have it (and its wonderful libraries) run on top of a more widely accepted runtime, or even a Scheme or CL.. UPDATE: here is a nice blog on function argument handling semantics in R, which turns out to be pretty complicated. UPDATE: I finally found the draft of the R Language Definition. The section on evaluation and argument matching is particularly interesting.
SML vs OCaml - excellent reference sheet
Lambdas in PHP
Does Visual Studio Rot the Mind?
Wednesday, February 25, 2009
JavaScript 1.8 starts to look like R: expression closures
(function(x) x+1)(1)These are expression closures in JavaScript 1.8.
return
is gone, good riddance.
Now if only we could write fun
or \
instead of function
!
The code would work in R, and my wish applies to that language as well.
FSharp.IsRecord
Trying it out:member IsRecord : typ:Type * ?bindingFlags:BindingFlags -> boolReturn true if the typ is a representation of an F# record type
> FSharpType.IsRecord(typeof<int*int>);; val it : bool = trueWhy would tuples be records, really?
Record Calculi. John C. Mitchell, Atsushi Ohori
Tuesday, February 24, 2009
Random sampling of views. MS SQL
Which Scheme?
GMAIL Outage?
Saturday, February 21, 2009
hdaemonize - for enunuchs demons in haskell
import Control.Concurrent
import System.Posix.Daemonize
main = daemonize $ \log ->
do flag <- newEmptyMVar
let stop = do log "STOPPING"
putMVar flag ()
let loop i = do threadDelay $ 10^6
writeFile "/tmp/counter" $ show i
loop $ i + 1
let start = do log "STARTING UP"
forkIO $ loop 0
takeMVar flag
let reload = log "RELOAD SIGNAL CAUGHT"
return $ Program start stop reload
This little program writes into /tmp/counter the number of seconds since its start, and reports its events to daemons.log; it is also ready to be deployed to /etc/init.d or /etc/rc.d; started by root, it will drop priviledges to the daemon user, or, if you name the program 'foo' and user foo exists, to user foo.
Far from being a UNIX guru, I am almost certain that I did some mistakes in the library, so I release it open-soure and welcome corrections.
hdaemonize home:
http://github.com/toyvo/hdaemonize/tree/master
inspiration for the code:
http://sneakymustard.com/2008/12/11/haskell-daemons