3

How can I convert a concatenated String to a Turtle FilePath? For instance, the following program tries to read some text files, concatenate them into a new one and remove the old ones. It does not seem to work although the OverloadedStrings extension is enabled:

{-# 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

The error thrown is:

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

Switching to output "example.txt" concat would just work fine. Isn't String just a type alias of [Char]?

jarandaf
  • 4,297
  • 6
  • 38
  • 67
  • 1
    `fromString` listed [here](http://hackage.haskell.org/package/turtle-1.2.8/docs/Turtle.html) might work. – pdexter Jun 21 '16 at 08:19
  • But you probably should be using turtle's `Format` interface found [here](http://hackage.haskell.org/package/turtle-1.2.8/docs/Turtle-Format.html) – pdexter Jun 21 '16 at 08:21

1 Answers1

6

String is just an alias to [Char], yes.

You see the bit where it says {-# OverloadedStrings #-}? What that does is make the compiler automatically insert fromString everywhere you write a literal string. It does not automatically insert it anywhere else you touch a string, only when it's a string constant.

If you manually call fromString on the result of the whole expression for building the path, that will probably fix it. (In particular, the show function always returns String, not any kind of overloaded string.)

MathematicalOrchid
  • 61,854
  • 19
  • 123
  • 220
  • I understand, thank you for reminding me this. And yes, using `fromString` automatically fixed the type error. – jarandaf Jun 21 '16 at 08:32