Showing posts with label Lisp. Show all posts
Showing posts with label Lisp. Show all posts

Friday, March 7, 2008

Freemind map <--> lisp (perl, python) list convertion (I,II)


Intro:


I am using Freemind on regular basis, for storing ideas, notes, and planning.It is an ideal tool for the manipulating tree like structures as simple, as it can be done with plain text, and even simpler, due to the Freemind intuitive shortcuts.

In fact, file in Freemind .mm can be processed as XML (if ignore some details, like header, and html that can present inside nodes, maybe something another can be also) using XSLT transform, or in any programming language with access to XML parsing.

I want lists structures being presented by Freemind, and back,
Freemind trees being presented in list-like structures, like following: ((1 2 ) 3 (4 (5 6)) 7 8).

Part I. list to freemind converter.



I have simply modified one of previously presented programs, there : nested list implementation

Instead of writing "[" I wrote <node> tag with empty TEXT attribute, simply for anonymous fork inside Freemind:
$ret .= "<node ID=\"Perl_GEN".$id."\" TEXT=\"".$listp."\" />\n";


Also beginning and header of freemind map written in corresponding places of program.
That is
<map version="0.9.0_Beta_8">
and closing of the tag.

For the list:
$al = [1, [2, [3], 4], 5];


I have obtained:


Part II. Freemind to plain list converter.


Lets start from presentation of Freemind tree nodes in one not nested lisp list (no round brackets, '(' and ')' inside list):

Lets look inside of Freemind map:
$ cat map.mm | head
First too lines describe program and format.
Than <node> tags follow.
We need only the text field from node.

So XSLT transformer of mm to plain list, ignoring tree structure can look like following:
xslt simple.

$ xsltproc map_lisp_list_s.xsl ~/map.mm | perl -e "<>; while(<>){print;}"
(
"map"
"check"
"check1"
"check2"
"check3"
"ch3ch1"
"node"
"subnode1"
"subnode2"
"trnode" )

Perl was simply used to strip one line from beginning.

Part III,

that is Freemind to tree list will be described further.

Monday, February 25, 2008

Links: differences in syntax: Perl, tcl, Shell, C++, Python, Java, Javascript, Lisp

I have already mentioned in my post on comparing numbers and strings in shell, about problems of simultaneous use of different programming languages.

To conclude, having reference cards with description one language to another differences can be useful.

Listed is set of resources, intended for migration from one language to another.

Recommended (short,self-descriptive, useful):
languages comparison:
http://merd.sourceforge.net/pixel/language-study/syntax-across-languages/


Recommended, but not short:

Wikipedia page:
http://en.wikipedia.org/wiki/Comparison_of_programming_languages

Open directory listing for comparisons:
http://www.dmoz.org/Computers/Programming/Languages/Comparison_and_Review/

PLEAC - Programming Language Examples Alike Cookbook
Comparison of productivity of writing in different programming languages:
page.mi.fu-berlin.de/~prechelt/Biblio/jccpprtTR.pdf

Useful:
Java for c++ Programmers:
http://pages.cs.wisc.edu/~hasti/cs368/JavaTutorial/

http://triton.towson.edu/~mzimand/os/Lect2-java-tutorial.html
Lisp to javascript converter, descriptive.
http://javascript.crockford.com/little.html
Another one Lisp to Javascript converter written in javascript.( You can look into source to look into the code)
http://www.joeganley.com/code/jslisp.html

Comparison Python with Java, Lisp i.t.c.

http://wiki.python.org/moin/LanguageComparisons


Three scripting concurrents:
http://mjtsai.com/blog/2002/11/25/perl_vs_python_vs_ruby/

Accumulator generator in different languages:
http://www.paulgraham.com/accgen.html

Tcl vs. Python, with nice short examples

http://homepages.cwi.nl/~sjoerd/PythonVsTcl-old.html


This thread describes differences between bash and perl.
http://www.perlmonks.org/?node_id=661859

And at the end resource with language comparison in action (memory, speed, size).
http://shootout.alioth.debian.org/

Friday, August 31, 2007

Iteration vs Recursion, Lisp vs C++, Functions vs Templates



1st chapter of SICP book task: recursive process in Scheme programming language without any loop forms (like for-each).

Implementation in Scheme:

;; recursive process
;; 1.11 from sicp
(define (f_rec1_11 n)
( cond ((< n 3) n )
(else (+
(f_rec1_11 (- n 1))
(f_rec1_11 (- n 2))
(f_rec1_11 (- n 3))))))

;; iterative (tail recursion)
(define (f_iter1_11 n) (f_i 0 1 2 (- n 2) ))

(define (f_i a0 a1 a2 count)
(if (= count 0) a2
(f_i a1 a2 (+ a0 a1 a2) (- count 1))))


Task in C: To implement without for, while, do, until, and goto output of numbers from one to 1000 in C.

#include <stdio.h>

int print_next_up(int in,int up)
{
printf("%d\n\n", ++in);
if (in >= up) return 0;
print_next_up(in,up);
}

int main()
{
print_next_up(0,1000);
return 0;
}


Example of the work of the C++ preprocessor:
Procedure in C++, printing numbers from 10 to 1 without even passing values to method calls or constructors. And recursion is in preprocessor.

#include <iostream>

template <int n> class printer
{
public :
printer() {
fun();
}
private:
int fun ()
{
std::cout << n << std::endl;
printer <n-1> myprinter;
}
};


template<> class printer<0>
{};

int main()
{
printer<10> a;
return 0;
}


And last, implementation of the task without loop constructs and recursion.

#include <iostream>

class my{
public:
static int a;
my()
{ std::cout << ++a << std::endl; }
};

int my::a = 0;

int main()
{
my myj[1000];
}