On 09/08/2014, at 10:24 PM, srean wrote:

> Looks great. Quick question, can array values (as opposed to array objects) 
> be created by recursion ?

Yes but it costs a fortune. You can do it in C++ too with vector. You have to 
make
a new array with one extra element each time.

A better question is: can you *decode* an array recursively?

And a better question than that is, can you decode a *tuple* recursively?

And the answer is yes. It was a lot of work to get this right.
The type

        A ** B

is a single term of type A, and a tuple B of at least two components.
So you can pattern match on the value for of this:

        ?h ,, ?t 

To see this in action go here:

http://felix-lang.org/share/lib/std/datatype/tuple.flx


> 
> Say I want the array (0,1,2,....42) is there a succinct way to write that. I 
> guess this would require some compile time execution.

That's not the question you asked though. Yes there is way to do this, in full 
generality,
using array comprehensions and generators, however I haven't encoded it in the 
library.
Could be an interesting job for you :)

instead I will show how to do it with lists:

@h2 List comprehension.
Make a list from an option stream.
Named variant.
@felix
  //$ List comprehension:
  //$ Make a list from a stream.
  fun list_comprehension[T] (f: (1->opt[T])) = {
    var ff = f;
    fun aux (l:list[T]) = {
      var x = ff();
      return 
        match x with 
       | Some ?elt => aux (Cons (elt,l)) 
       | None => rev l
       endmatch
      ;
    }
    return aux Empty[T];
  }

@h2 List comprehension.
Make a list from an option stream.
Constructor variant.
@felix
//$ List comprehension:
  //$ Make a list from a stream.
  ctor[T] list[T](f: (1->opt[T])) => list_comprehension f;

So now to use it:

gen f() : opt[int] = 
{
  for i in 0 upto 42 do yield Some i; done
  return None;
}

var hitchhiker = list f; 

Clearly you could then just make a varray from the list:

        var a = varray hitchhiker;

To make a raw array is harder because the length has to be know at
compile time. You would have to allocate the array uninitialised and then
fill it. But it shouldn't be too hard.

> 
> Are array values compile time constants ?

No, but the length has to be known at compile time for a raw array.

> Or can they be created at runtime but cannot be changed once created.

Values can't be changed in principle. However all values can be stored
in variables which makes them objects and thus mutable, provided
you have some mutators. Roughly once in an variable the value
is addressable and so you need some function taking a pointer
to the type which can be passed to a function that modifies it.

It's hard talking about arrays in Felix because there are at least 6 klnds
of arrays: raw arrays, C pointers, carrays, varrays, darrays, pointers
to raw arrays .. and probably a few more :)

Raw arrays are tuples at the moment and compiler supported.
The other stuff is in the library, either as a concrete by abstract
data type, or, as an abstract model with some instances,
or some weird combination.

It's hard to simplify because we have to deal with C stuff as well.

but see the next post ..

--
john skaller
skal...@users.sourceforge.net
http://felix-lang.org




------------------------------------------------------------------------------
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to