Clojure中的Tribonacci功能

时间:2016-02-25 14:32:32

标签: clojure

是否有一种不同的,更清洁的方式来生成tribonacci?

private OnClickListener Cart = new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        cart = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map = new HashMap<String, String>();
            map.put(TAG_PID, pid);
            map.put(TAG_NAME, name);
            map.put(TAG_CATEGORY, category);
            map.put(TAG_PRICE, price);
            map.put(TAG_IMAGE,String.valueOf(resId));
            cart.add(map);

            AddtoCart obj = (AddtoCart) getApplicationContext();
            obj.setCart(cart);

        Intent a = new Intent("com.example.pb.VIEWCART");
        startActivity(a);
    }
};

2 个答案:

答案 0 :(得分:0)

以下是我的写作方式:

(defn tribonacci [a b c]
  (lazy-seq (cons a (tribonacci b c (+' a b c)))))

如果您想获取 n th tribonacci号码,只需使用(tribonacci 0 0 1),然后在其上调用nth

答案 1 :(得分:0)

你可以创建这些数字的延迟序列,并从中获取所需的元素:

user> (def t (lazy-cat [1 1 2] (map +' t (rest t) (nthrest t 2))))
#'user/t
user> (take 20 t)
(1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136 5768 10609 19513 35890 66012)

[1 1 2]替换为[0 1 1],如果您需要0