orangeduck/BuildYourOwnLisp

Loops

Opened this issue · 5 comments

zmmaj commented

how to make loop function in buildYourOwnLisp ,eg.
for()
while()
repeat()
any help?

Wouldn't you build those out of LISP itself, adding to your "prelude", don't think you need to bake those into the builtin's (though I'm sure it is possible).

zmmaj commented

Ah.. I was try.. but without any progress. That is why I need help.. example or similar..
But, your post may help.. i think.
I will try something as you wrote.
(fun {do & l} {
if (== l nil)
{nil}
{last l}
})

that's smell as repeat() function eg. repeat number of times(function or calculation)
Problem is harder.. You see.. repeat function need to send output after every loop. while(), for() doing same...
whatever I wrote whenever my new function send output they broke self.
Your above function is OK.. they send out only last l .. but loop functions send's output after every loop.
how to wrote to resume on next loop without stopping self?
Sorry I am not expert in english. , I hope You will understand what is a problem.

I'd query why you want a for/while/repeat, not being obtuse, but in general, I'd stick to map, fold and reduce functions for functional programming. e.g. in Scala, I don't think I've ever used a while loop....
That said, I think it is fairly fundamental that you don't (author can correct me if I'm wrong) implement those things in the C, rather the C is effectively a simple extended lambda-calculus engine of sorts, and the prelude should implement the higher level constructs. The builtin is I think supposed to be just enough for you to build a prelude in your language.

zmmaj commented

I try to put these code on higher level...
I am game maker programmer.. A expert for older version of gamemaker from gm 4 to gm 8.1..
So, this can be interested 'cose GM studio 1 and 2 was very veeery slow while compile code.
Older version was been written in Python, now that is all ported to C++, and it's NOT GOOD. Too slow... not game.. compiling. For me as programmer that is horror...
To be short,
When you create eg sky with stars you will need repeat, or while, or similar instead to write codes for every star on sky.
I was import SDL codes here, and they works... Nothing big.. about 10-15 commands, but I was stop when I realise that I can't to do many things who are depend from loops.
eg, checking collision, positions etc,
That is not cruicial only for games..
So, if IS possible, please help me to make loop functions, built in, or external...
tnx in advance.

zmmaj commented

Ok.. I was make built-in function REPEAT
I need While, for, etc etc loop functions

lval* builtin_repeat( lenv* e, lval* v) {
LASSERT_NUM("repeat", v, 2);
LASSERT_TYPE("repeat", v, 0, LVAL_NUM);
LASSERT_TYPE("repeat", v, 1, LVAL_QEXPR);
v->cell[1]->type = LVAL_SEXPR;
int broj= (int)v->cell[0]->num;
lval* res;
lval* x;
lval* y;
int i = 0;
lval* l[broj];
while (broj > i){
y= lval_copy(v);
res= lval_pop( y, 1);
x = lval_eval(e, res);
l[i] = x;
lval_println(x);
x=NULL;
y=NULL;
res=NULL;
i++;
}

lval_del(v);
return l[1];
}

USAGe
repeat 5{ print "aa"}
repeat 5{100}
repeat 10 { + 2 3}
etc etc
and YES.. I was make and function for random number.