how to assign to this List

Category: visual studio fsharp

Question

Map to new space on Tue, 14 Oct 2014 11:36:10


i want to fix output as type list<Object>

but when i assign it, got some difficulty

let mmult(m : list<Object>) : list<Object> =
    let mutable result : list<Object> = []
    for i in 0..((m.[0] :?> list<Object>).Length) do // aRow
        for j in 0..(((m.[1] :?> list<Object>).[0] :?> list<Object>).Length) do // bColumn
            for k in 0..(((m.[0] :?> list<Object>).[0] :?> list<Object>).Length) do // aColumn
                    ((result.[i] :?> list<Object>).[j] :?> list<Object>) <- ((result.[i] :?> list<Object>).[j] :?> list<Object>)  :: [(A.[i,k], B.[k,j])]
    result


Near the end of programming era

Replies

Mr. Tines on Tue, 14 Oct 2014 16:31:24


This is the language telling you that this is the wrong way to try and implement matrix multiplication.

If these heterogeneous list representations are some sort of serialized form, consider a design that adds a proper deserialization to a more appropriate form, like an Array2D for manipulation, and then re-serializes as needed.

Map to new space on Wed, 15 Oct 2014 06:40:43


how to do as you said serialize and deserialize, 

i just want to testing a design idea that all use fixed type List<Object> -> List<Object>

to be the most generic that no matter how i ordering the program steps then it still can run without error

when i see the list.item, I sense that there is a need for priority of functions, otherwise list.item should not be a member of self modifying program in database


Near the end of programming era

Mr. Tines on Thu, 16 Oct 2014 18:23:04


The code fails because the expression to the left of the assignment is not mutable.

If you were to fix that, then it would fail at runtime because you never actually initialise the result value with a list of empty lists so that there is any value at i,j to be a target of assignment.

Then you will find that the cons operator :: is not what you meant.

You will eventually arrive at something like this

let mmult(m : list<Object>) : list<Object> =
    let mutable result : list<Object> = []
    for i in 0..((m.[0] :?> list<Object>).Length) do // aRow
        let mutable ilist : list<Object> = []
        for j in 0..(((m.[1] :?> list<Object>).[0] :?> list<Object>).Length) do // bColumn
            let mutable jlist : list<Object> = []
            for k in 0..(((m.[0] :?> list<Object>).[0] :?> list<Object>).Length) do // aColumn
                    jlist <- jlist @ [(A.[i,k], B.[k,j])]
            ilist <- ilist @ [jlist]
        result <- result @ [ilist]
    result;;
and run it to find that the performance is atrocious, and you would have been better using a properly mutable data-structure, and not concatenating immutable lists.