2016-02-26 124 views
1
空白項

比方說,我聲明一個變量在GHCI列表中刪除

let a = ["hello","goodbye","","sup","bye","hi","cat","dog"] 

而且我想着手去除討厭的「」從列表中。從我的理解,我應該能夠使用dropWhile這樣做,這樣

dropWhile null a 

但這不會刪除討厭的「」。爲什麼是這樣?我該如何解決這個問題?

啊,現在我明白了。我完全誤解了dropWhile的工作原理。那麼爲了讓這個問題有點不同 - 我如何刪除列表中的空白條目?

回答

9

dropWhile當給出謂詞時滴元素爲Truenull "hello"False,所以,它應該放棄任何東西。

您可能正在尋找一個filter

Prelude> filter (not . null) ["hello","goodbye","","sup","bye","hi","cat","dog"] 
["hello","goodbye","sup","bye","hi","cat","dog"]