Long but good post on Clojure - http://bc.tech.coop/blog/081201.html
Technorati Tags: clojure, erlang
Technorati Tags: clojure, erlang
This is Sid's Blog on Lisp, Scheme and everything in between.
stack(S) ->
io:format("Stack:~p~n", [S]),
receive
{push, PID, Data} ->
PID ! success,
stack([Data|S]);
{pop, PID} ->
if
S == [] ->
PID ! failed,
stack(S);
true ->
[H|T] = S,
PID ! {success, H},
stack(T)
end
end.
PANIC: unprotected error in call to Lua API (no calling environment)So, I went to the Lua Users Wiki and found some tutorials but still no "hello world" program. So, here it is below
The code itself is self explanatory and the API interface is very interesting. A stack based approach to pass information to/from C from/to lua.
#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
int main(int argc, char* argv[])
{
char buff[] = "print(\"hello world\")";
int error;
lua_State *L = lua_open();
luaL_openlibs(L);
error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
lua_pcall(L, 0, 0, 0);
if(error)
{
printf("ERROR: %s", lua_tostring(L, -1));
lua_pop(L, 1);
}
lua_close(L);
return 0;
}
(define (power-n x)The above definition basically defines a generator function. A slightly different definition using a named let is
(define power-x
(lambda (n)
(if (= n 0)
(list 1)
(cons (power x n) (power-x (- n 1))))))
power-x)
(define (power-n x)In the first case, we define power-x and then explicitly return it. In the second version, we create an anonymous function and return that automatically.
(lambda (n)
(let power-x ((n n))
(if (= n 0)
(list 1)
(cons (power x n) (power-x (- n 1)))))))
(define (power-two n)Thanks to a thread on comp.lang.scheme for fixing things!
((power-n 2) n))