상호 작용을 사용하여 Haskell 프로그램을 만들려고합니다. 가장 많이 사용 된 단어와 횟수가 반환됩니다. 정렬과 예제를 보았습니다 -하지만 모든 단어에 대한 개수를 알 필요가 없습니다, 나는 단지 가장 반복 된 단어가 필요합니다. 지금까지 나는 가지고있다 :Haskell - 상호 작용이있는 목록에서 가장 반복되는 요소를 계산합니다.
import Data.List -- (sort)
import Data.Char -- (isAlpha, toLower)
import Data.Ord -- (maximumBy)
main =
interact
$ unwords
-- comment: here show the size of the list and the word (probably head)
. maximumBy(comparing length)
. group
. sort
. words
. map (\char -> if isAlpha char then toLower char else ' ')
위의 컴파일. 시간 "the"
텍스트에 표시되는 단어의 수를
[the, the, the, the, the, the, the, the...]
; maximumBy
는 다음과 같이 가장 많이 사용되는 단어를 제공합니다 나는 "the"
이 내가 제공 한 텍스트에서 가장 많이 사용되는 단어임을 확인했습니다.
import Data.List -- sort
import Data.Char -- isAlpha, toLower
import Data.Ord -- maximumBy
main =
interact
$ unwords
. map (\(n, w) -> show n ++ ", " ++ show w)
. map (\s -> (length s, head s))
. maximumBy(comparing length)
. group
. sort
. words
. map (\char -> if isAlpha char then toLower char else ' ')
:
"the, 318"
나는 첫 번째 문자 "T"와 3을 제공하는 다음과 같은 시도 :
이 같은 내가 출력하고자입니다
"3, 't' 3, 't' 3, 't' 3, 't' ..."
누구나 내가 뭘 잘못하고 있는지 알아?
도움 주셔서 감사합니다. 이것이 제가 시도한 해결책이었습니다. – Zorkolot