On Sat, 2007-08-25 at 17:12 -0700, Erick Tryzelaar wrote:
> This is so cool. Here's very very basic list comprehensions:
>
> comprehensions.flx:
> syntax comprehensions {
> satom := lsqb sexpr rsqb =># (List::list _2);
> satom := lsqb sexpr vbar sname <- sexpr rsqb =># """
> `( ast_apply
> ,_sr
> ( ( ast_apply
> ,_sr
> ( ( ast_lookup (( ast_name "List" () ) "map" () ) )
> ( ast_lambda
> (
> ( () ( ( ast_tuple () ) () ) )
> ( ( ( ( PVal ,_4 ( ast_name "int" () ) ) ) none ) )
> typ_none
> ( ( ast_fun_return ,_sr ,_2 ) )
> ) ) ) )
> ( ast_name "xs" () ) ) )
> """;
> }
> Now, I just need to figure out how to not hardcode the name of the
> list and the type of the inner function. If I replace the "xs" with
> ,_6, it errors with:
Of course it does, but that's because you're an idiot :)
Why do you want to replace the name? You don't. You want
an arbitrary expression: replace
( ast_name "xs" () )
with
,_6
and it works fine:
val ys = [ x * 3 | x <- [1,2,3] ];
works!
Replacing the inner function so the type is polymorphic
is impossible because it isn't. The type of
x * 3
in the example depends on the type of x, so you must specify
the type of x.
If you know the list on the RHS of the -> symbol is non-empty,
then you can probably do this with a typematch .. something like:
typematch typeof (list (1,2,3)) with
| list [?t] => ?t
endmatch
looks like it should work .. but probably doesn't. A simpler
solution is just
[ x * 3 | x : int <- [1,2,3] ]
which also works for an empty list.
--
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Felix-language mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/felix-language