在Haskell中返回一个String的第一行 [英] Return the first line of a String in Haskell

查看:119
本文介绍了在Haskell中返回一个String的第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当简单的问题,我很难过。基本上,我只需编写一个函数,它接受一个字符串,将它分解成行,接受第一行并返回第一行格式正确的HTML标题标记。



我真的只是不知道从哪里开始。



任何东西都会有帮助。



我确实有代码,但这只是我用来对输入文件进行转换的一些基本功能:

  convertToHTML: :字符串 - > 
convertToHTML cs0 =
case cs0
('#':'#':cs) - > < H2> 中++ cs ++< / h2>
('#':cs) - > < H1> 中++ cs ++< / h1>
--- - > < HR /> 中
_ - > cs0

convertToHTML':: String - > String
convertToHTML'= unlines.map(convertToHTML.firstLine.escapeChars).lines

convertToWords :: String - >字符串
convertToWords cs1 =
case cs1
('*':'*':cs) - > <强> 中++ cs ++< / strong>
('_':'_':cs) - > <强> 中++ cs ++< / strong>
('*':cs) - > < EM> 中++ init cs ++< / em>
('_':cs) - > < EM> 中++ init cs ++< / em>
_ - > cs1

convertToWords':: String - >字符串
convertToWords'= unwords.map convertToWords.words

这些是基本功能,我我正在调用我的main,它读取输入文件,调用函数并生成一个输出文件。

  main = do 
args< - getArgs - 命令行参数
let(infile,outfile)=(\(x:y:ys) - >(x,y))args
putStrLn $输入文件:++ infile
putStrLn $输出文件:++ outfile
contents< - readFile infile
writeFile outfile $ deleteSymbol $ convertToWords'$ convertToHTML'$ contents

希望您能得到代码的要点。 >解决方案

要检索第一行,您可以简单地使用:

  firstLine :: String  - > ;字符串
firstLine = head。行

然后您可以使用:

  firstLineInTitleTags :: String  - > String 
firstLineInTitleTags s = concat [< title>,firstLine s,< / title>]

或:

  firstLineInTitleTags :: String  - >字符串
firstLineInTitleTags =(< title>++)。 (++< / title>)。 firstLine


I have a fairly simple question that I am stumped on. Basically, I just have to write a function that takes a string, breaks it into lines, takes the first line and returns the first line in properly formatted HTML title tag.

I honestly just don't know where to start with this.

Anything will help.

I do have code, but this is just some of the basic functions that I am using to do the transformations on the input file:

    convertToHTML :: String -> String
    convertToHTML cs0 = 
          case cs0 of
                ('#' : '#' : cs) -> "<h2>" ++ cs ++ "</h2>"
                ('#' : cs)       -> "<h1>" ++ cs ++ "</h1>"
                 "---"            -> "<hr/>"
                  _                -> cs0

    convertToHTML' :: String -> String
    convertToHTML' = unlines.map (convertToHTML.firstLine.escapeChars).lines

    convertToWords :: String -> String
    convertToWords cs1 =
                case cs1 of
                      ('*' : '*' : cs) -> "<strong>" ++ cs ++ "</strong>"
                      ('_' : '_' : cs) -> "<strong>" ++ cs ++ "</strong>"
                      ('*' : cs)       -> "<em>" ++ init cs ++ "</em>"
                      ('_' : cs)       -> "<em>" ++ init cs ++ "</em>"
                      _                -> cs1

    convertToWords' :: String -> String
    convertToWords' = unwords.map convertToWords.words

these are the basic functions that I am calling from my main, which reads the input file, calls the functions and produces an output file.

    main = do
          args <- getArgs -- command line args
          let (infile,outfile) = (\(x:y:ys)->(x,y)) args
          putStrLn $ "Input file:  " ++ infile
          putStrLn $ "Output file: " ++ outfile
          contents <- readFile infile      
          writeFile outfile $ deleteSymbol $ convertToWords' $ convertToHTML' $ contents

hopefully you get the gist of the code.

解决方案

To retrieve the first line you can simply use:

firstLine :: String -> String
firstLine = head . lines

then you can use:

firstLineInTitleTags :: String -> String
firstLineInTitleTags s = concat ["<title>", firstLine s, "</title>"]

or:

firstLineInTitleTags :: String -> String
firstLineInTitleTags = ("<title>"++) . (++"</title>") . firstLine

这篇关于在Haskell中返回一个String的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆