这是我在这里的第一个问题所以如果我做错了什么请告诉我。
我正在尝试构建一个脚本
所以,对于这样的输入: “[1,2,3]” “[5,24,3]” “[1,3,4,5]” “[0,8,6]”
输出应该是 “[1,5,1,0]” “[2,24,3,8]” “[3,3,4,6]” “[5]”
这是我目前所拥有的:
import Data.List
import Control.Exception
import System.IO
import System.Environment (getArgs)
-- Task 4, 5, 6
-- This is the zip function that operates differently
multiZipL :: [[a]] -> [[a]]
multiZipL [] = []
multiZipL (pps) = [p | p<- map head (pps)] : multiZipL (nltail (pps))
nltail:: [[a]] -> [[a]]
nltail [] = []
nltail xss = filter (not.null) $ map tail xss
-- splitOn helps out with processing each list separately
splitOn :: Char -> String -> [String]
splitOn c [] = []
splitOn c ls = (takeWhile (/=c) ls) : splitOn' c (dropWhile (/=c) ls)
where splitOn' c [] = []
splitOn' c (x:[]) | x==c = [[]]
splitOn' c (x:xs) | x==c = splitOn c xs
| otherwise = []
-- Error handling for improper file
fileMZip :: String -> IO ()
fileMZip fileName = catch (fileMZip' fileName) filehandler
filehandler :: IOException -> IO ()
filehandler e =
do let err = show (e :: IOException)
hPutStr stderr ("Warning: Couldn't open input/output file : " ++ err)
return ()
-- This block does the zipping and the printing to a new file
fileMZip' fileName =
do s <- readFile fileName
let ls = lines(s)
let sss = map (splitOn ',') ls
let iss = map (map read) sss :: [[Int]]
let mss = multiZipL iss
let oss = map (map show) mss
let css = intercalate "\n" (map (intercalate ",") oss)
writeFile "ziplists.csv" css
main :: IO ()
main = do (fileName : _ ) <- getArgs
fileMZip fileName
我不确定问题是出在文件名处理上,还是我在编译文件时没有提供任何输入。老实说,我不太确定应该在哪里提供文件名作为输入。
我注意到,在用 ghc 编译后,可执行文件因以下语句而崩溃:
t4.exe: user error (Pattern match failure in 'do' block at t4.hs:46:11-25)
对应这个区块
main = do (fileName : _ ) <- getArgs
fileMZip fileName```
Can you please help me out with this? Thanks in advance :D