Author Topic: (Teaser) A new pseudocode/prototype language  (Read 5599 times)

0 Members and 1 Guest are viewing this topic.

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9672
Re: (Teaser) A new pseudocode/prototype language
« Reply #15 on: December 06, 2011, 02:35:36 PM »
Awesome! Thank you for the adjustment.


> (Completely guessing at the pseudo code)

You're a natural! Your code produced exactly what you wanted it to.
Code - Auto/Visual Lisp: [Select]
  1. (defun replace( aList  position  newItem )
  2.   (if aList
  3.     (if (> position 0)
  4.       (cons (car aList) (replace (cdr aList)(- position 1)newItem))
  5.       (cons newItem (cdr aList))
  6.       )
  7.     )
  8.   )
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

BlackBox

  • King Gator
  • Posts: 3747
Re: (Teaser) A new pseudocode/prototype language
« Reply #16 on: December 06, 2011, 03:10:27 PM »
Awesome! Thank you for the adjustment.


> (Completely guessing at the pseudo code)

You're a natural! Your code produced exactly what you wanted it to.

You're very welcome; that is kind of you to say.

Cheers! :beer:
"How we think determines what we do, and what we do determines what we get."

BlackBox

  • King Gator
  • Posts: 3747
Re: (Teaser) A new pseudocode/prototype language
« Reply #17 on: December 06, 2011, 07:19:20 PM »
As a simple matter of efficiency (which recursive functions are not exactly known for), and as a simple point of I guess you could say the "elegance" of LISP... I think you'll find this variation even more robust, and efficient.

The replace2 function, in addition to that which the replace function already does, is to check that all arguments are non-Nil, and to ensure that the user does not attempt to replace a value within a list of lesser length (meaning that the fifth item of a list of three cannot be replaced).

My recent variation (posted above):

Code - Auto/Visual Lisp: [Select]
  1. (defun replace  (aList position newItem)
  2.   (if aList
  3.     (if (> position 0)
  4.       (cons (car aList) (replace (cdr aList) (- position 1) newItem))
  5.       (cons newItem (cdr aList))
  6.       )
  7.     )
  8.   )

Current offering:

Code - Auto/Visual Lisp: [Select]
  1. (defun replace2 (aList position newItem)
  2.   (if (and aList
  3.            position
  4.            newItem
  5.            (>= (1- (length aList)) position))
  6.     (subst newItem (nth position aList) aList)
  7.     )
  8.   )

Bench test results from VLIDE Console (100K iterations):

Code - Auto/Visual Lisp: [Select]
  1. _$
  2.  
  3. REPLACE
  4. _$
  5.  
  6. REPLACE2
  7. _$
  8. _$ (bench '(replace replace2) (list '("FOO1" "FOO2" "FOO3") 2 "NEW") 100000)
  9.  
  10. REPLACE
  11. Elapsed: 608
  12. Average: 0.0061
  13.  
  14. REPLACE2
  15. Elapsed: 562
  16. Average: 0.0056
  17. _$

Taking another hand-typed stab at the pseudo code syntax:

Code - C++: [Select]
  1. def replace(aList position newItem)
  2.    if and(aList, position, newItem, >=((1-(length(aList))), position)) then
  3.         subst(newItem, nth(position, aList), aList)
  4. end;

Enjoy!

"How we think determines what we do, and what we do determines what we get."

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9672
Re: (Teaser) A new pseudocode/prototype language
« Reply #18 on: December 06, 2011, 08:27:59 PM »
Oh, I didn't build in support for '>='...

For the record, the language formatting is really flexible--meant to allow you to format your code anyway you want to help you read it better-. For example, math functions are meant to be typed outright. like so:

This:
Code: [Select]
1 + 2 - 4 * 5 - 1
Would produce this:
Code - Auto/Visual Lisp: [Select]
  1. (- (- (+ 1 2) (* 4 5)) 1)
So, we can have either of these (this won't work right at this moment because I forgot all about '>=' and the like).
Code - C++: [Select]
  1. def replace (aList position newItem)
  2.    if and(aList, position, newItem, length(aList) - 1 >= position)
  3.       then
  4.         subst(newItem, nth(position, aList), aList)
  5. end;

Code - C++: [Select]
  1. def replace (aList position newItem)
  2.    if and(aList,
  3.           position,
  4.           newItem,
  5.           length(aList) - 1 >= position)
  6.    then
  7.        subst(newItem,
  8.              nth(position, aList),
  9.              aList)
  10. end;

TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

BlackBox

  • King Gator
  • Posts: 3747
Re: (Teaser) A new pseudocode/prototype language
« Reply #19 on: December 06, 2011, 09:02:41 PM »
No worries; I can appreciate that aspect.

For clarity "1+" and "1-" are valid (LISP) functions, hence my usage. LoL

As for the other functions and operators, I believe the other thread has a good list of most (if not all) functions and operators, no?

That reminds me that I've still not finished that VB.NET.php file for you... things are hectic right now. *Shrug* LoL
"How we think determines what we do, and what we do determines what we get."

Lee Mac

  • Seagull
  • Posts: 12502
  • London, England
Re: (Teaser) A new pseudocode/prototype language
« Reply #20 on: December 07, 2011, 06:03:37 AM »
[ Aside: ]

Current offering:

Code - Auto/Visual Lisp: [Select]
  1. (defun replace2 (aList position newItem)
  2.   (if (and aList
  3.            position
  4.            newItem
  5.            (>= (1- (length aList)) position))
  6.     (subst newItem (nth position aList) aList)
  7.     )
  8.   )

However, note that subst will replace all occurrences of an item:

Code: [Select]
_$ (replace2 '(1 2 3 3 2 1) 3 4)
(1 2 4 4 2 1)

An alternative for your consideration:

Code: [Select]
(defun replace3 ( alist position newitem / i )
    (setq i -1)
    (mapcar '(lambda ( x ) (if (= position (setq i (1+ i))) newitem x)) alist)
)

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1544
  • Moscow (Russia)
Re: (Teaser) A new pseudocode/prototype language
« Reply #21 on: December 07, 2011, 06:26:35 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun f (l a b)
  2.   (cond ((< a 2) (cons b (cdr l)))
  3.         ((cons (car l) (f (cdr l) (1- a) b)))
  4.   )
  5. )
Stay home. Stay safe. Save lives.

BlackBox

  • King Gator
  • Posts: 3747
Re: (Teaser) A new pseudocode/prototype language
« Reply #22 on: December 07, 2011, 07:57:32 AM »
However, note that subst will replace all occurrences of an item:

Ohhh; I did fail to consider that in my limited example. *oops*

Good catch, Lee... Thanks for the correction.
"How we think determines what we do, and what we do determines what we get."

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: (Teaser) A new pseudocode/prototype language
« Reply #23 on: December 07, 2011, 11:45:49 AM »
Se7en, your code reminds me of some mixed breed between Pascal and Python. Less verbose than Pascal, but not as much as in Python / Lisp.

E.g. your test of an empty list using
Code: [Select]
if null(aList) then
  ...
Would strictly be translated in Lisp to
Code: [Select]
(if (null aList)
  ...
However Python disallows this non-standard comparison, e.g. a direct translation would be
Code: [Select]
if null = aList
  ...
In both Lisp and Python its recommended to use the idea that an empty list evaluates to false. So the more "correct" translation would be
Code: [Select]
(if (not aList)
  ...
Code: [Select]
if not aList
   ...

But I suppose that's just splitting hairs  :pissed: .

Though it shows one of the aspects where non-lispers have difficulty figuring out what's going on. Apart from no then and else keywords, in lisp you'd use the falseness of an empty list to your advantage and thus re-arange your if statement. Something like this:
Code: [Select]
if null(aList) then
  ... do whatever is required when the list is empty;
else
  ... do whatever's to be done with a list containing at least one value;
Translated to lisp:
Code: [Select]
(if aList
  (... do whatever's to be done with a list containing at least one value)
  (... do whatever is required when the list is empty)
)
Which makes the back-translation even more problematic. You either need to re-arrange the if portions, which you can't always do since there may not be an else portion. Or you need to change the (if aList into
Code: [Select]
if not (null (aList)) then
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Jeremy

  • Guest
Re: (Teaser) A new pseudocode/prototype language
« Reply #24 on: December 07, 2011, 03:57:22 PM »
I'm glad to see I'm not the only crazy person trying to create their own language. You might find my approach interesting. I write my code as an MTEXT entity in a dwg file and translate it into a lsp file. This allows me to do all my annotating with AutoCAD itself and I can draw diagrams (even 3D ones) right next to the code as god intended. Why use some lousy 2D editor when we have AutoCAD itself! Long lists I create as a table and reference simply in my code. This moves large bothersome pieces over to the side so that the main features of the program are still visible. Navigation is far easier because you can now pan over or zoom in and out. Move your code pieces arround in any pattern you want and drop color patches behind them to visually group them. Do what you want.

I can write functions with multple arguments in my language because they are translated out as functions that take the arguments in as a list. So (+ 2 3) would be translated to (lwp-+ '(2 3)) and lwp-+ would be a rewritten AutoLISP function that you would load in amongst others containing all of the AutoLISP functions rewritten to take in lists of arguments. My translator does the dirty work of turning the arguments into lists. This is my way of bypassing Autodesk's negligence on improving LISP. I am not ready for unleashing this yet as there is much I must do. I call my language LIWP (LIsp Without Parentheses). Actually it doesn't get rid of all of the parentheses but it does get rid of the left ones.