Arc Forumnew | comments | leaders | submitlogin
Core language addition suggestions
2 points by JeremyDunn 6363 days ago | 2 comments
In the spirit of Paul Graham's request for core language ideas here are a few that I use in NewLISP that he might find useful:

1. RTN RTN (short for RETURN) takes a boolean expression and returns the first argument to the expression rather than returning the truth value. For instance, if we write (rtn = 2 2) then 2 is returned, this is the same as (if (= x 2) x). I find that in boolean tests that the truth value is just a means to an end and this function bypasses the need to write a conditional statement to get the value.

2. LENGTH? How often have you written (if (= (length lst) n)... I have written a function where I write this as (length? lst n =). If no n then 1 is assumed and if no operation than = is assumed. Thus to test if a list's length is equal to 1 we can write (length? lst). Is the length less than 3 then we write (length? lst 3 <).

3. FMAP FMAP is very handy. Suppose we have (* (+ a b)(+ c d)(+ e f)), then we can write this in the alternate form of (fmap * + a b ^ c d ^ e f) wbere the carets act as spacers for the arguments.

4. IFLENGTH This is a new conditional statement that takes a list, a length to test against and up to three blocks of code A,B,C. If we write (iflength lst n A B C) then this is the same as (if (< n (length lst)) A (= n (length lst)) B C). If we write (iflength lst n A B) this is the same as (if (= n (length lst)) A B). And finally (iflength lst n A) is just (if (= n (length lst)) A).

I have found all of these very handy.



3 points by bogomipz 6363 days ago | link

Number 1 gave me an idea; it would be useful if (is 2 2) returned 2 rather than t. The only edge case seems to be (is nil nil) which must return t and not nil...

-----

1 point by sjs 6361 days ago | link

Using = to test for equality is confusing in an Arc forum. :)

1. This could be added without introducing a new reserved word. I have a git branch on my machine here where < and > return their first argument if the comparison succeeds.

2. Seems handy. I would put the operator before the number to compare just because it reads better, though I realize the op varies the least. The default of testing (is (len lst) 1) seems arbitrary, but I guess (no lst) takes care of the most common length test so maybe it's useful. Also, (is 1 (len lst)) is only 3 chars longer than (length? lst). Personally I like the ? suffix to denote predicate fns but I don't think pg does (or he would use them). For Arc I would suggest (len? ...).

I'm not sure about 3 and 4.

4. It seems backwards to put the (< n (len lst)) case first, no? I would put (< (len lst) n) first, then (is (len lst) n), then (> (len lst) n).

-----