2016-06-21 35 views
3

如何將連接的String轉換爲Turtle FilePath?例如,下面的程序試圖讀取一些文本文件,將它們連接成一個新文件並刪除舊文件。它似乎沒有工作雖然OverloadedStrings擴展啓用:將字符串轉換爲Turtle.FilePath

{-# LANGUAGE OverloadedStrings #-} 

module Main where 

import System.Environment 
import System.IO 
import Control.Monad 
import Turtle 
import Turtle.Prelude 
import qualified Control.Foldl as L 

main :: IO() 
main = do 
    params <- getArgs 
    let n    = read $ params !! 0 
     k    = read $ params !! 1 
    -- Some magic is done here 
    -- After a while, read generated .txt files and concatenate them 
    files <- fold (find (suffix ".txt") ".") L.list 
    let concat = cat $ fmap input files 
    output (show n ++ "-" ++ show k ++ ".txt") concat 
    -- Remove old .txt files 
    mapM_ rm files 

拋出的錯誤是:

Couldn't match expected type ‘Turtle.FilePath’ 
       with actual type ‘[Char]’ 
    In the first argument of ‘output’, namely 
     ‘(show n ++ "-" ++ show k ++ ".txt")’ 

切換到output "example.txt" concat也只是正常工作。是不是String只是[Char]的類型別名?

+1

'fromString'列出[here](http://hackage.haskell.org/package/turtle-1.2.8/docs/ Turtle.html)可能工作。 – pdexter

+0

但是您可能應該使用turtle的'Format'接口找到[這裏](http://hackage.haskell.org/package/turtle-1.2.8/docs/Turtle-Format.html) – pdexter

回答

6

String只是[Char]的別名,是的。

你看到它說的那個位{-# OverloadedStrings #-}?所做的就是讓編譯器在你編寫文字字符串的任何地方自動插入fromString。它不會而不是只有當它是一個字符串常量時,纔會自動將其插入觸摸字符串的任何位置。

如果您手動調用fromString關於構建路徑的整個表達式的結果,那可能會修復它。 (特別是,show函數總是返回String,而不是任何類型的重載字符串。)

+0

我明白了,謝謝提醒我這一點。是的,使用'fromString'自動修復了類型錯誤。 – jarandaf