λ's profile(λabcdefghijklmnopq·fgha...BlogListsGuestbookMore Tools Help

(λabcdefghijklmnopq·fghabc)logicProgramming

Lambda is the force, Herbrand is the source.
July 02

'Hello world!' in yet another language.

Some may guess with this “goals” what is the language.

write(‘Hello world!’),nl.

Yes, prolog is my yet another language to learn.
Let see what are those languages that I’ve learn so far?

Batch files – In old fashion MS-DOS,it’s my very first programming experience.
QBASIC – By helping my dad, I’ve learn more complete programming language.
Pirch and mIRC script – of course, IRC was the most funniest thing on internet 18 years ago(Beside, hacking, nuke, mail bomb and blah blah).
Visual Basic – My first windows programming.
Visual Basic.NET – Have to learn this for my part-time job(Very differ from VB).
Delphi – I learn this because,at that time, the most of the “Trojan horse” programs were written in Delphi.
Foxpro – For creating my high-school manage teaching schedules.
C on Unix – to modify an IRC daemon code, and later I’ve managed to create a new service GodServ.
C++ – Thank for my university teaching this,I believe that this language give me a complete view of OOP not the Java.
Java – Base on my experience from C++, Java is very easy and beautiful.
Perl – My first web programming
PHP – Early scripting language for web.I’d been trying to build Object PHP for a year before PHP formally support.
C# – After got an annoy feeling of “primitive” and “object type” from Java, I love this language.It’s consistent.
Javascript - made me realize that what in the world this script could do.
LISP – A plenty of parentheses.
Scheme – from SICP give my a lambda feeling.
Ruby – Learn from web application class.
Python – Have to learn this for implementing Chord protocol for Computer network project.
F# – My first functional programming experience that really practical on .NET.
OCaml – An F# ancestor.
.NET CIL – To write an executable file for my compiler.
Prolog – Yet another language to learn for my thesis!

ps. All of languages listed above are the ones I have really used it and create a real complicated application not just peeked at their syntax and says “hey, I know this language”

From now on, Prorock!!!

March 21

Continuation-style passing

#light

open Parser
open Lexer
type expr =
    | Num of int
    | Add of expr * expr
    | Sub of expr * expr
    | Mul of expr * expr
    | Div of expr * expr
    | Bracket of expr
    | NegBracket of expr
    with
        static member (+) (x,y) = Add(x,y)
        static member (-) (x,y) = Sub(x,y)
        static member (*) (x,y) = Mul(x,y)
        static member (/) (x,y) = Div(x,y)

//Define function name alias
let readLine = System.Console.ReadLine

printf "Please enter an expression to evaluate\n:>"

let input = readLine()//Get User Input

let parseExpr str =
    try    
        Lexing.from_string str |> Parser.expr Lexer.token
    with e ->
        failwithf "Error : %s" e.Message
        

//LR Top-down Continuation style evaluation the syntax tree.
let eval st =
    let rec parse tree cont =
        match tree with
        |Add(a,b) -> parse a (fun l -> parse b (fun r -> cont (l+r)))
        |Sub(a,b) -> parse a (fun l -> parse b (fun r -> cont (l-r)))
        |Mul(a,b) -> parse a (fun l -> parse b (fun r -> cont (l*r)))
        |Div(a,b) -> parse a (fun l -> parse b (fun r -> if r <> 0 then cont (l/r) 
else failwith "Divide by zero")) |Bracket e -> cont (parse e (fun x->x)) |NegBracket e -> cont (-(parse e (fun x->x))) |Num a -> cont a parse st (fun x -> x) let ast = parseExpr input ast |> printfn "AST : %a" output_any ast |> eval |> printfn "value : %d" readLine()

---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
Below is the lexical definition

{
open Parser
}

let num = ['0'-'9']+

rule token = parse
    |num    {NUM (Int32.of_string (Lexing.lexeme lexbuf)) }
    |'+'    { ADD }
    |'-'    { SUB }
    |'*'    { MUL }
    |'/'    { DIV }
    |'('    { OPEN }
    |')'    { CLOSE }
    |eof    { EOF }
    |_        {failwithf "unrecognized input: '%s'" (Lexing.lexeme lexbuf) }

March 05

Syntax-Directed Translator Series : Simple Calculator F# Lexical analyzer

Hola!

So long, no update
Thesedays, I’m trying implement basic calulator on F#.

--------------------------------------------------------------------------------

#light

//Define function name alias
let write (x:string) = System.Console.Write(x)
let writeLine (x:string) = System.Console.WriteLine(x)
let readLine = System.Console.ReadLine
let parseInt = System.Int32.Parse
let digitCheck = System.Char.IsDigit

writeLine("Please enter an expression to evaluate")
write(":>")
let input = readLine()//Get User Input
let charStream = List.of_seq input //decompose a string into char list

type token = //token type definition
    |NUM of int
    |ADD |MUL |SUB |DIV
    
let lexical stream = //char stream -> token stream
    let rec buildNumber num list = //number handling using basic recursion
        match list with
        |c::tail when digitCheck c -> 
            if tail = [] then (num+c.ToString(),[]) 
            else buildNumber (num+c.ToString()) tail
        |tail -> (num, tail)
            
    let rec lex = function //token matching
        |c::list when digitCheck c -> (buildNumber "" (c::list)) |> 
                                      (fun (n,l) -> (NUM (parseInt n))::(lex l))
        |'+'::list -> ADD::(lex list)
        |'*'::list -> MUL::(lex list)
        |'-'::list -> SUB::(lex list)
        |'/'::list -> DIV::(lex list)
        |[] -> []
        |_ -> failwith("Unexpected pattern in lex list")
    lex stream
    
lexical charStream |> print_any //perform lexical on char stream and print to console

readLine() |> ignore //For only break console window before it shutdown
December 23

Revise your C++ Dynamic Link Library

C++ is one of the most complicated languages, however, knowing C++ is omnipotent to deal with almost any programming issues.C++ DLL is a common way to reduce the code duplication and make it easier to update the deployed applications by just only update those out of date version.

You may need some analgesic before completely erudite with C++ DLL especially if you are very new to C++, but I would say that it’s not that hard, actually C++ and all underlying concepts are comparatively easy.All the scabrousness are came from flexibility, compatilibity, extensibility and optimization.

This entry is about revising basic of C++ DLL if you familiar with C/C++, you are likely comfoartable with this.

DLL loading in two methods:
- static loading
- dynamic loading

The major disparity is when a DLL is statically loaded, it needs to load to program memory at the starting time.While dynamic loading,which give more flexibility, can be load at anytime while the client(loader) program is running.

In a normal C program, we need entrypoint of program, similarly, in DLL we also need entrypoint for a DLL to initialize an appropriated setting before use.DLLMain is the entrypoint that every DLL have to declare.The following code segment is a usual way to define DLLMain.

BOOL APIENTRY DllMain( HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
    switch(ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

The switch code block is the reason to tell the callee DLL to initailize configuration with appropriate environment(attach or detech, process or thread)

And after the DLLMain have define, we can add a function to be use by others just like a normal function in C++ program.

__declspec
(dllexport) int HelloWorld(){
    cout << "Hell-o-World!";
    return0;
}

and in header file(.h)
__declspec(dllexport) int HelloWorld();

Above code is my example which declare hello world function, you may notice the peculiar keyword “__declspec(dllexport)”.This keyword is just used by compiler to indicate that this function is exported as DLL function by using standard convention.

Ok, now, to use the function that I have define, I client program code I just add the reference to the DLL which have complied from above code and by adding this line

extern "C"__declspec(dllimport) int HelloWorld();

int _tmain(int argc, _TCHAR* argv[])
{
    HelloWorld();
    return 0;
}

to define the function from external DLL then we done static loading DLL.
We will review for danamic loading in next entry which is a bit more complicated.
Check the attached file for example code written in VS2008.

LiveJournal Tags: ,,
December 20

Functional adventure story

If you have some experience about using functional programming in your real-life and want to share those good (or bad) experiences.It's your time. You may share whether functional approach success or fail in the real world.
please follows the link: ICFP: tell your stories

 
F# for Scientists
Expert F# (Expert's Voice in .Net)

Planet F#

Loading...Loading...
Link to the extreamly very interesting research groups.
Thanks for visiting!
Would you mind leave your sign?

Please wait...
Sorry, the comment you entered is too long. Please shorten it.
You didn't enter anything. Please try again.
Sorry, we can't add your comment right now. Please try again later.
To add a comment, you need permission from your parent. Ask for permission
Your parent has turned off comments.
Sorry, we can't delete your comment right now. Please try again later.
You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
Complete the security check below to finish leaving your comment.
The characters you type in the security check must match the characters in the picture or audio.
ยินดี ที่ได้เข้ามาเจอ Blog ดีๆ อย่างนี้นะครับ
6 days ago
ผ่านมา เลยลงชื่อครับ เป็น Blog ที่เยี่ยมครับ
June 9