factorial n = product [1..n]
factorial := n\(1..n) product
: factorial ( n -- n ) [1,b] product ;
×/″ι″N
Hmpf. I didn't really understand any of those, and that's part of my problem with these languages.
My language would ideally be as verbose as possible, but not excessive with keystrokes. Basically, I would minimize the number of non-alphabetical symbols used.
define factorial as
function n (default 5, n is a Number)
n*(factorial n-1)
factorial 5
map factorial over [1,2,3]
factorial (factorial 5)
Another totally new feature I'm mulling over is a special syntax for "units". So I should be able to define something like this:
define unit in as
function n
new Inch n
move 5in => move (new Inch 5)
Also, instead of classes, I'd prefer having a kind of "factory" which generates a custom dictionary object, kind of like JS but sleeker:
define type Inch as
{
_initialize_:function self n (n is a Number, default 1)
set self.value to n
toFeet: function self
self.value/12
}
(new Inch 5).toFeet
Notice how in many functions, I have "label text". That's intentional, and should behave like BYOB/Snap!. I want to be able to make special functions like
define map as
function f (f is a Function) "over" l (l is a List)
set newL to EMPTY_LIST
for i in l
add (f i) to newL
return newL
map
function n
n*2
over [1,2,3,4,5]
And while I'm on special forms, how about this?
define for as
function i (upvar) "in" j (j is Iterable) "do" f (unevaluated)
// implementation skipped over...
for item in [1,2,3]
print item*10
(bad example, but it shows the point).
Don't ask me how I'm planning to parse all that stuff, but I think it's both possible and worth it.
In other news, I managed to get Snap! file I/O working haphazardly, and have managed to raytrace a nice, patriotic "S". Now to figure out why it's half-sunken into the ground...
Last edited by Hardmath123 (2013-01-29 06:01:13)
Offline
Hardmath123 wrote:
define map as
function f (f is a Function) "over" l (l is a List)
You've put a lot of thought into this, and have some really interesting ideas. (I especially like units, although it's going to be a little painful to get arithmetic of values with units correct.) But in the example above, why do you treat the word map so differently from the word over? They're both part of the name of the function, and privileging the first word rules out infix operators. How about
define function
map (Function f) over (List l)
as
...
This wouldn't have to be strongly typed; you could say
define function
map (f) over (l)
In other news, I managed to get Snap! file I/O working haphazardly
By running a Snap! server on your own computer, or did you persuade a browser to do file I/O?
Offline
factorial n = product [1..n]
factorial := n\(1..n) product
: factorial ( n -- n ) [1,b] product ;
×/″ι″N
Fewest keystrokes, eh?
OK, I'm using Snap!. I click the "make a block" button, type "!" (1 keystroke), make an input called n (1 keystroke), then do
report <if <(n) = (0)> then (1) else ((n) * <((n) - (1)) !>)>(i'm using the if/then/else block in the tools library)
Last edited by joefarebrother (2013-01-29 15:51:37)
Offline
bharvey wrote:
Hardmath123 wrote:
define map as
function f (f is a Function) "over" l (l is a List)You've put a lot of thought into this, and have some really interesting ideas. (I especially like units, although it's going to be a little painful to get arithmetic of values with units correct.) But in the example above, why do you treat the word map so differently from the word over? They're both part of the name of the function, and privileging the first word rules out infix operators. How about
define function
map (Function f) over (List l)
as
...
This wouldn't have to be strongly typed; you could say
define function
map (f) over (l)
So I would reference a function as map:over: selector-style? That's one thing I really don't like about languages like Obj-C and Squeak. Names of functions and any other variables should be the same, so that really functions are just variables. So the first "word" in a "sentence" is always some expression which returns a function, and the rest are arguments. An argument can optionally also be part of the "label text", in which case it is only present for readability: "set x to 5" makes more sense than "set x 5".
Actually, I only have 3 types of definitions: define unit, define type (classes!), and plain ol' define. The arguments to define are just the name and the value. So in the example, I was assigning a lambda to the name map. I'm using the latter of the below:
(define (f x) (x)) (define f (lambda (x) x))
In other news, I managed to get Snap! file I/O working haphazardly
By running a Snap! server on your own computer, or did you persuade a browser to do file I/O?
Python server.
Offline
Hardmath123 wrote:
Names of functions and any other variables should be the same, so that really functions are just variables. So the first "word" in a "sentence" is always some expression which returns a function, and the rest are arguments. An argument can optionally also be part of the "label text", in which case it is only present for readability: "set x to 5" makes more sense than "set x 5".
Is the "to" required or optional (in a call)?
One of the advantages we get from title text beyond the first word is that we can sort of overload names, e.g., we have
for (i) = (1) to (10)
and we also have
for each (item) in (list)
and this works because the full names of the procedures include the entire text (and the places for inputs). It sounds like you can't do that in your language, because "for" is just a variable that has one procedure as its value.
Offline
Hardmath123 wrote:
Another totally new feature I'm mulling over is a special syntax for "units". So I should be able to define something like this:
define unit in as
function n
new Inch n
move 5in => move (new Inch 5)
Yeah, I made a language that did that a while ago with some friends. The syntax was just:
unit m (for base units)
unit mm = 1e-4 m (for prefixes)
unit N = kg m / s^2 (for derived units)
And it would do calculations and unit conversions for you:
> 10 m/s^2 * 5.3 hours / 3 inches in MHz
< 2.50393701 MHz
(it also had lambdas, which is why I said "language" and not "fancy calculator")
The problem with using "new" is that it's likely to mislead C/Java programmers, and doesn't actually make a new anything—it just attaches a unit.
Regarding the label text discussion: I've always thought the way Python did it was nice. All arguments can be passed as named arguments or positional arguments, and functions can take variable amounts of both named and positional arguments. If you have an assignment operator, though, you'll have to use a syntax different from (@bharvey ) func(a, b, c=d, e=f)
If you want to use labels interspersed between arguments (and not just named arguments), I wrote a scratchblocks parser that is reasonably skilled at determining what you mean without hordes of parentheses (e.g., `set a to item 2 + 3 * 4 of positions + 10' instead of `set [a] to ((item ([2] + ([3] * [4])) of [positions v]) + [10])'), and can send you the source/give you some help if you need any.
Last edited by nXIII (2013-01-29 15:47:31)
Offline
nXIII wrote:
If you want to use labels interspersed between arguments (and not just named arguments), I wrote a scratchblocks parser that is reasonably skilled at determining what you mean without hordes of parentheses (e.g., `set a to item 2 + 3 * 4 of positions + 10' instead of `set [a] to ((item ([2] + ([3] * [4])) of [positions v]) + [10])'), and can send you the source/give you some help if you need any.
That sounds clever. Can I see?
Offline
blob8108 wrote:
nXIII wrote:
If you want to use labels interspersed between arguments (and not just named arguments), I wrote a scratchblocks parser that is reasonably skilled at determining what you mean without hordes of parentheses (e.g., `set a to item 2 + 3 * 4 of positions + 10' instead of `set [a] to ((item ([2] + ([3] * [4])) of [positions v]) + [10])'), and can send you the source/give you some help if you need any.
That sounds clever. Can I see?
It's PHP and it's not hosted anywhere.
Offline
bharvey wrote:
nXIII wrote:
unit N = kg m / s^2 (for derived units)
If this:
is a centimeter, what's this:
?
An upside-down centimeter? Sorry, I don't understand your question.
Last edited by nXIII (2013-01-29 22:13:03)
Offline
bharvey wrote:
Hardmath123 wrote:
Names of functions and any other variables should be the same, so that really functions are just variables. So the first "word" in a "sentence" is always some expression which returns a function, and the rest are arguments. An argument can optionally also be part of the "label text", in which case it is only present for readability: "set x to 5" makes more sense than "set x 5".
Is the "to" required or optional (in a call)?
Required, but there's a special function called "run":
define f function x y z return x + y + z run f with arguments 10, 11, 12 run f with list [1,2,3]
Also,
run f with current continuation
One of the advantages we get from title text beyond the first word is that we can sort of overload names, e.g., we have
for (i) = (1) to (10)
and we also have
for each (item) in (list)
and this works because the full names of the procedures include the entire text (and the places for inputs). It sounds like you can't do that in your language, because "for" is just a variable that has one procedure as its value.
I don't want to do that either, then it's not as intuitive to pass functions as arguments (in Obj-C you use @selector(a:b:c which is really complicated and yucky. They also have "blocks" which are like lambdas except very strictly typed. The compiler infers the return type. ).
Offline
nXIII wrote:
Hardmath123 wrote:
Another totally new feature I'm mulling over is a special syntax for "units". So I should be able to define something like this:
define unit in as
function n
new Inch n
move 5in => move (new Inch 5)Yeah, I made a language that did that a while ago with some friends. The syntax was just:
unit m (for base units)
unit mm = 1e-4 m (for prefixes)
unit N = kg m / s^2 (for derived units)
And it would do calculations and unit conversions for you:
> 10 m/s^2 * 5.3 hours / 3 inches in MHz
< 2.50393701 MHz
(it also had lambdas, which is why I said "language" and not "fancy calculator")
The problem with using "new" is that it's likely to mislead C/Java programmers, and doesn't actually make a new anything—it just attaches a unit.
Regarding the label text discussion: I've always thought the way Python did it was nice. All arguments can be passed as named arguments or positional arguments, and functions can take variable amounts of both named and positional arguments. If you have an assignment operator, though, you'll have to use a syntax different from (@bharvey ) func(a, b, c=d, e=f)
If you want to use labels interspersed between arguments (and not just named arguments), I wrote a scratchblocks parser that is reasonably skilled at determining what you mean without hordes of parentheses (e.g., `set a to item 2 + 3 * 4 of positions + 10' instead of `set [a] to ((item ([2] + ([3] * [4])) of [positions v]) + [10])'), and can send you the source/give you some help if you need any.
"New" basically creates a new instance of a class. So Inch is a class, and the unit "in" is actually a lambda which takes an argument and returns an Inch object with the argument. That way I can have any arbitrary type of unit, not limited to numbers, including things like [0,5]point or [255,255,230]color. That basically makes readability easier:
move 5in forward square at [0,0]point side 3px turn 5deg ccw turn (math.PI)rad cw
Kind of like CSS!
Offline
Hardmath123 wrote:
"New" basically creates a new instance of a class. So Inch is a class, and the unit "in" is actually a lambda which takes an argument and returns an Inch object with the argument.
…so calling is postfix?
That way I can have any arbitrary type of unit, not limited to numbers, including things like [0,5]point or [255,255,230]color. That basically makes readability easier:
Code:
move 5in forward square at [0,0]point side 3px turn 5deg ccw turn (math.PI)rad cwKind of like CSS!
In CSS, units are always attached to scalar values. This just looks like an alternate form of constructor. Are you planning on doing unit conversions?
Offline
A millimeter? A millipede?
Offline
nXIII wrote:
In CSS, units are always attached to scalar values. This just looks like an alternate form of constructor.
Yes, I suppose it is kind of like a constructor, but more like an abbreviation.
Are you planning on doing unit conversions?
Yeah, otherwise why not just use raw numbers?
Offline
Hardmath123 wrote:
That way I can have any arbitrary type of unit, not limited to numbers, including things like [0,5]point or [255,255,230]color.
That's not really much different from "[0,5] Point" -- just a postfix notation for constructors. Point and color aren't really units at all. (Well, point is, if you mean 1/12 of a pica, but not the kind of point you mean.) Units have a whole algebra; they come in categories (distance units, time units, etc.) that interact in funny ways. You can add unit-tagged numbers of the same category (inches and meters, say) but not of different categories (inches and hours). But you can multiply inches by hours, in principle, although it makes more sense to divide inches by hours (to get the speed of a turtle ). And if you multiply two units of the same category you get a result in square whatevers.
Offline
Noooo… the suspense…
Well, ideally I'd have Python-style wrappers (_mul_,_add_,_sub_,_div_) so that units can be custom-mixed, too, so that I can multiply colors:
red-blue=purple
or something.
Offline
Hardmath123 wrote:
so that units can be custom-mixed, too
The thing is, real units have a whole set of common structure that aren't shared with things like colors. Just off the top of my head, a category (say, distance) has subcategories based on systems of units (metric, English), so if you add feet to meters you should get an answer in meters, but if you add feet to inches you should get an answer in inches (although a really smart implementation would notice if the answer you get is either extremely large or extremely small and switch units accordingly). And there'd be a database of conversions across categories, so (to take one of n's examples) an answer in sec^-1 would be converted to Hz.
If you conflate units with other types, you're losing all that structure, which you then have to build by hand.
Offline
Hardmath123 wrote:
My language would ideally be as verbose as possible, but not excessive with keystrokes. Basically, I would minimize the number of non-alphabetical symbols used.
Try inform
I know it doesn't suit your purpose, but it is very interesting.
Offline