jeudi 6 juillet 2017

If statements(no returns) in Pharo Smalltalk for flatten list

I'm trying to make a list(30) of lists (30). I have this:

#(#(a a a) nil nil #(b) #(c) #(d) nil nil nil #(e e) nil #(f f) nil #(g) 
  #(h) nil nil nil #(i  i) nil nil nil #(j) nil nil nil nil nil 
  #(k) #(l l l)

And I want to do a program that turns that into just a list like this:

 #(a a a b c d e e f f g hi i j k l l l)

My code is this:

ToList: lista
    | retorno y z |
    retorno := Array new: 30.
    y := 1.
    (1 to: lista size)
        do: [ :i | 
            z := 0.
            (lista at: i) isNil
            ifFalse: 
            (1 to: (lista at: i) size do: [ :j | retorno at: (y + z) put: ((lista at: i) at: j )
                y:= y+1.
                z:= z +1.]
            )].
        ^retorno

I think there are some problems with the if-statements, because there's no return. I don't know what to do, because it works without the y:= y+1., but it keeps some nil spaces.

Please, help me!

Aucun commentaire:

Enregistrer un commentaire