Common Lisp中的两个求和函数有什么区别,谢谢

时间:2018-10-27 00:44:56

标签: lisp common-lisp elisp

两个代码示例都是求和公式的: Code examples are for the summation formula

代码示例1

public Tile[] TileSprite(Tile[]Tiles)
{
    int TilesNum = Tiles.Length;

    for (int i = TilesNum - 1; i > 0; i--) 
    {
        Tiles[i].Image = Resources.Load<Sprite>("TileSprites/" + Tiles[i].Name); 
    } //Running through my assets in Unity and finding sprites to match up with tile


    return Tiles;
}



public void PopulateTiles()
{


    Tile Unnamed = new Tile(0, "Unnamed", 0, false, null);
    Tile SpaceStationFloor1 = new Tile(1, "SpaceStationFloor1", 50, true, null); 
    Tile SpaceStationWall1 = new Tile(2, "SpaceStationWall1", 100, true, null);
    Tile Thrusters = new Tile(3, "Thrusters", 75, true, null);

    Tile[] TilesInitialized = new Tile[] {
        Unnamed,
        SpaceStationFloor1,
        SpaceStationWall1,
        Thrusters
    };  //Creating Tiles here ^^^^
    //Plugging in my list to get corresponding sprites \/
    TilesInitialized = TileSprite(TilesInitialized); 
    AddToList(TilesInitialized); //Sending list to fucntion to add them to tile's list

}

private static void AddToList(Tile[] TilesInitialized)
{

    for (int i = TilesInitialized.Length - 1; i >= 0; i--)
    {
        Tile Newtile = TilesInitialized[i]; 
        Tile.Tilelist.Add(Newtile); //Where I run into my issue
    }
}
private void Start()
{
    PopulateTiles();        
    Instantiate(Resources.Load("Tile1"), new Vector3(0f,0f,0f), Quaternion.identity);
}

代码示例2

(defund sigma (func n)
        (cond ((= n 1)(funcall func 1)
        (t (+ (sigma func(1- n))
           (funcal func n))))))

1 个答案:

答案 0 :(得分:4)

  

两个代码示例都是求和公式的

不,他们没有。第二个数字相加,第一个数字以数字作为参数调用一个函数,并对结果求和。 在数学符号中的sigma之后,将有f(i)而不是i。在高阶函数术语中,它是项函数。以下是一些使用10的示例:

(sigma (lambda (v) 1) 10) ; ==> 10 in CL I'd use (sigma (constantly 1) 10)
(sigma #'1+ 10)           ; ==> 65
(sigma #'identity 10)     ; ==> 55

第二个只会产生第三个示例:

(sigma 10) ; ==> 55

PS:您的函数有语法错误和错别字,我刚才已经忽略了。您需要先修复这些问题,然后才能起作用。最难的可能是在第一个cond项中缺少结尾括号,从而使cond仅具有一个项,而第二个函数也传递了func,自其版本以来没有意义只接受一个论点。