Season 1 아카이브/프로그래밍
                
              Functional Programming with Haskell - Strings are [Char]
                문장전달자
                 2018. 4. 19. 18:44
              
              
                    
        728x90
    
    
  하스켈에서 문자열은 단순히 문자의 리스트이다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17  | Prelude> "testing" "testing" it :: [Char] Prelude> ['a'..'z'] "abcdefghijklmnopqrstuvwxyz" it :: [Char] Prelude> ["ust","a","test"] ["ust","a","test"] it :: [[Char]] Prelude> ["just","a","test"] ["just","a","test"] it :: [[Char]]  | cs | 
리스트에서 사용가능한 함수는 모두 문자열에서도 사용할 수 있다.
1 2 3 4 5 6 7 8 9 10 11 12  | Prelude> let asciiLets = ['A'..'Z'] ++ ['a'..'z'] asciiLets :: [Char] Prelude> length asciiLets 52 it :: Int Prelude> reverse (drop 26 asciiLets) "zyxwvutsrqponmlkjihgfedcba" it :: [Char]  | cs | 
1 2 3 4 5 6 7  | Prelude> :type elem elem :: (Eq a, Foldable t) => a -> t a -> Bool Prelude> let isAsciiLet c = c `elem` asciiLets isAsciiLet :: Char -> Bool  | cs | 
Prelude 에서는 문자열을 [Char] (a type synonym)로 정의한다. 아래를 보자.
1 2 3 4  | Prelude> :info String type String = [Char]    -- Defined in ‘GHC.Base’  | cs | 
문자열에는 다수의 함수를 적용할 수 있는 대표적인 2가지 함수 words 와 putStr 을 보자.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19  | Prelude> let z = "Just some words!" z :: [Char] Prelude> words z ["Just","some","words!"] it :: [String] Prelude> tail(words z) ["some","words!"] it :: [String] Prelude> unwords (tail (words z)) "some words!" it :: String Prelude> putStr (unwords (tail (words z))) some words!it :: ()  | cs | 
- words : words breaks a string up into a list of words, which were delimited by white space.
 - unwords : unwords is an inverse operation to words. It joins words with separating spaces.
 - putStr : Write a string to the standard output device (same as hPutStr stdout).
 
728x90