2016-04-07 122 views
6

我有一個球拍列表,其中有些值爲(list 'foo 'bar 2 #t 42 9 2 'some)。實際上,這些價值觀遵循一些更具體的模式,但對於這個問題來說,這是無關緊要的。我想測試列表中是否有兩個相同的值,在這種情況下是數字2,並獲取元素和其他元素。這是我的嘗試:如果無數列表中的兩個值相同,則匹配

#lang racket 

(match (list 'foo 'bar 2 #t 42 9 2 'some) 
    [(list-no-order a a rest ...) 
    "Do some stuff"] 
    [_ "Do some other stuff"]) 

該模式是(list-no-order a a rest ...)。但程序的解釋失敗:

a11: unbound identifier; 
also, no #%top syntax transformer is bound in: a11 

對我來說,它在轉換宏時看起來是一個錯誤。如果將list-no-order更改爲list,則該模式將起作用,但當然只有當元素位於列表的開頭時才適用。

我的模式是錯誤的,如果是的話如何糾正它,或者是不可能的預期模式,什麼是解決它的最佳方式?

+2

這裏是'(匹配宏展開(表2 2#T)(列表無秩序ASD ASD後的結果dsa)「做一些事情」])':http://pastebin.com/K3PG44kY。我們可以看到有一個未綁定的id asd8。也許這個宏是越野車? –

+0

你有多確定這是一個錯誤?我應該向球拍開發者報告嗎? – Pyfisch

+1

報告在[Github](https://github.com/racket/racket/issues/1304)。 – Pyfisch

回答

0

我想知道爲什麼你想模式匹配的東西。通過你的問題和代碼,我不清楚它。我將通過純粹的列表處理辦法的問題(至少據我所知)

(filter 
    (lambda (x) 
     ;;filter for the first element of the prev created tuple and 
     ;;check if its larger than 1 
     (> (first x) 1)) 
    (map 
     (lambda (x) 
      ;;tuple of the length of the prevously created group and the group itself 
      (cons (length x) x)) 
     (group-by 
      ;;just the element it seld 
      (lambda (x) 
       x) 
      (list 'foo 'bar 2 #t 42 9 2 'some)))) 
相關問題