Looking for Cool new Language

If you want to discuss about Oforth...

Looking for Cool new Language

Postby joekillian » 17 Jul 2015 20:50

Greetings!

I do a lot of computer language recreational browsing, spending a lot of time at Rosetta Code and other forums. That's how I came across OForth which looks very cool. I like the idea of words/functions dominating over variables and the idea of point free/variable free programming in general. Factor looks interesting but OForth looks more interesting because its a new and original Forth.

I like the idea of stack based languages but wonder how easy it would be to eliminate the need for stack manipulation words by just using efficient local variables. Of course most of the time its just take values off the stack and return values for processing - pipelining - but there are times where a lot of stack flipping goes on in a lot of the Forth related language code at Rosetta. To what degree can good use of variables completely eliminate the need to use any stack manipulation words?

Can "dedicated" built in variable names for example be used to almost completely avoid the need for "declaring variables" and at the same time avoid almost all stack manipulation? The programming language J for example for its function definition uses "x" and "y" as set aside variable names. For functions that apply functions other dedicated letters are used. What about x1,x2,x3,x4,x5..........as dedicated local variable names for a Forth language like OForth?

So the OForth word for standard deviation which is "the square root of the average of the squares of the deviation from the mean" would be something like:

STD: x1 mean x1 - sq mean sqr

where x1 is just the array of numbers and mean, sq, sqr are built ins that work on arrays of numbers.

place x1 on the stack
place the mean of x1 on the stack
subtract mean from x1
square all those values on the stack
average those values
take the square root.

contrast this (STD: x1 mean x1 - sq mean sqr) with the J version which is a lot more verbose even though J,K etc. are supposed to very compact languages (and difficult to read)


mean=: +/ % #
dev=: - mean
stddevP=: [: %:@mean *:@dev

notice how "mean" is variable/point free but now requires you to know a rather abstruse parsing rule.

I think the reason why Forth languages have been as slow to adopt as J,K etc. is simply because of the stack flipping that goes on which with using local variables is not needed. This is exactly the same thing that goes on with J where in the interest of "NOT" using variable names long one line Tacit variable free code is written which can be very difficult to read.

So if dedicated local variable names/words can be used to eliminate the usual stack flipping words why not do that?

Perhaps OForth already can do this but if so why use the stack flipping words - for a bit more efficiency?
joekillian
 
Posts: 7
Joined: 17 Jul 2015 20:13

Re: Looking for Cool new Language

Postby Franck » 23 Jul 2015 11:35

Greetings ! (And sorry for this late answser).

Your question is interesting, and not only for Oforth but all stack based languages.

Oforth allows to declare variables (parameters or local variables).
And this is not an Oforth specificity : Forth also allows local variables.

For instance, in Oforth, without stack manipulation :

: std(x) { x map(#[ x avg - sq ]) avg sqrt }

(Not optimized : x avg result should be stored into a local variable).


If you declare one (or n) parameter, it will be removed from the stack and stored into the function
environnement. After that, it can be pushed on the stack.


So the question is : why still use the stack flipping words ?

And the answser is not easy at all and there are lots of discussion on this subject
in the Forth community.

Data stack is a core feature for Oforth. Even if you use local variables, the data stack
is here (you push them on the stack, ...).


For some, it is not a choice between stack flipping words and local variable uses.
Forth (and Oforth) is all about factoring words. When you do this efficiently, you can
avoid, most of the time, stack juggling and local variables.

For other, local variable are usefull when an object is used multiple times.

Well, there are lots of positions on this subject... :)

And for me, it is just a question of feeling about a word :
- Is the word is correctly factorized ?
- Is the word is readable ? And after 6 months ?
- Do I need a local variable because a closure is returned ?
- ...

Sorry to not be more specific but it is difficult to answer to this (interesting) question.

Franck
Franck
 
Posts: 145
Joined: 29 Oct 2014 19:01


Return to Discussions

Who is online

Users browsing this forum: No registered users and 1 guest

cron