0

これは、私が IBM Swift サンドボックスで楽しみのために行っていることです。私は最近、Scala で L-system algae を作成しましたが、言語がどのように比較されるかを確認するために Swift で実行するのはクールだと思います。

https://github.com/I2obiN/L-System-Algorithms-and-Fractals/blob/master/algae.scala

これは、私が何を目指しているかを示すための Scala であり、これが Swift にあるものです。

/**
  * Created by t.hood on 26/01/16
  * L-System Algae
  *
  */

 import Foundation

 // Mapping function for string

 func stringmap(x: String) -> String {
 var str = x;

     // Replace characters in string
     str = x.stringByReplacingOccurrencesOfString("A", withString: "AB") +
     str.stringByReplacingOccurrencesOfString("B", withString: "A");

  // Return mapped string;
  return str;
 }

 func lsys() {

 // Declarations

 var iteration : Int = 2;
 var x = 0;
 var lsystem: String = "A";
 let charA: Character = "A";
 let charB: Character = "B";

 while(x != iteration) {

 print(lsystem)

  // Iterate through characters in string
  for chars in lsystem.characters {
    lsystem = stringmap(lsystem);
  }

  // Inc count ..
  x+=1

 }
}

 // Run ..
 lsys();

私が抱えている問題は、マッピング機能にあります。x をマップし、結果を出力してから、str で次のマップを実行するために必要です。問題は、両方のマップの間に演算子 + があり、最初の操作の後に str をすばやく印刷できないことです。

これを回避する方法について誰かアイデアはありますか?最初の置換後に str を出力できれば、うまくいくと思います。

4

1 に答える 1

0

I created a project in Xcode so I could use the debugger to see what's going on. Here's what I found.

I moved your print statement into the for loop so it would show the value of lsystem every time the loop executed.

The while loop executes twice, when x == 0 and 1 The first time through the while loop lsystem == 'A', so the for loop executes once. In stringmap(), the 'A' becomes 'ABA'. 'ABA' is printed. The second time through the while loop, the for loop gets executed three times. For loop 1: 'ABA' is sent to stringmap() and 'ABBABAAA' is returned. For loop 2: 'ABBABAAA' is sent to stringmap() and 'ABBBABBABABABAAAAAAAA' is returned. For loop 3: 'ABBBABBABABABAAAAAAAA' is sent to stringmap() and 'ABBBBABBBABBABBABBABABABABABABABABAAAAAAAAAAAAAAAAAAAAA' is returned.

I modified your stringmap() function to iterate through the string character by character and apply the grammar to each character, appending each change to a new string that was then returned to lsys().

Seven iterations of the while loop returned this, which agrees with what I see in the Wikipedia article about L-system. Iteration 0: A Iteration 1: AB Iteration 2: ABA Iteration 3: ABAAB Iteration 4: ABAABABA Iteration 5: ABAABABAABAAB Iteration 6: ABAABABAABAABABAABABA Iteration 7: ABAABABAABAABABAABABAABAABABAABAAB

Here's the code. This worked in a playground on my iMac, I'd expect it to work in the IBM Swift sandbox too.

func stringmap(x: String) -> String
{
    var returnString = ""

    for char in x.characters
    {
        switch (char)
        {
        case "A" :
            returnString += "AB"
            break
        case "B":
            returnString += "A"
            break
        default:
            break
        }
    }
    return returnString
}


func lsys()
{

    // Declarations

    let iteration : Int = 7;
    var x = 0;
    var lsystem: String = "A";

    print("Iteration \(x): \(lsystem)")

    while(x != iteration)
    {
        lsystem = stringmap(lsystem)
        x++
        print("Iteration \(x): \(lsystem)")

    }
}

lsys()
于 2016-03-22T20:31:19.627 に答える