简体   繁体   English

对字符串使用haskell map函数

[英]Using the haskell map function with a string

I'm trying to use the map function in haskell 我正在尝试在haskell中使用map函数

I've got this: 我有这个:

lexi :: String -> [[String]]
lexi x = map (words.lines) x

I want to be able to put a string in to x, so it can be run like this 我希望能够将字符串放入x,因此可以像这样运行

lexi ("string here")

But get the error 但是得到错误

Couldn't match type ‘[Char]’ with ‘Char’
Expected type: String -> String
  Actual type: String -> [String]
In the second argument of ‘(.)’, namely ‘lines’
In the first argument of ‘map’, namely ‘(words . lines)’

Couldn't match type ‘Char’ with ‘[Char]’
Expected type: [String]
  Actual type: String
In the second argument of ‘map’, namely ‘x’
In the expression: map (words . lines) x

I know that if I use 我知道如果我使用

lexi = map (words.lines) 

it works fine when I run lexi ("string here") , but need the variable to use later on 当我运行lexi ("string here") ,它工作正常,但需要稍后使用的变量

Could some please explain why this doesn't work and how to fix it? 可以请一些人解释为什么它不起作用以及如何解决?

Thank you :) 谢谢 :)

This answer refers to an old version of the question . 该答案是该问题的旧版本

So let's get this quite clear (please always add the type signature of all functions you're talking about!) 因此,让我们弄清楚这一点(请始终添加您正在谈论的所有函数的类型签名!)

function :: Char -> [String]

Well, then the type of map function is [Char] -> [[String]] , ie String -> [[String]] . 好吧,然后map function的类型为[Char] -> [[String]] ,即String -> [[String]] But you want the result to be only [String] , not a triply-nested list. 但是您希望结果仅是[String] ,而不是三重嵌套的列表。 You probably want to join the lists of two levels together; 您可能希望将两个级别的列表一起加入; in general the function to use for list-joining purposes is concat (or more generally, join from the Control.Monad module). 在一般用于列表接合目的的功能是concat (或更一般地, joinControl.Monad模块)。 In this case, you have two different options: 在这种情况下,您有两个不同的选择:

  • Join the result of each call to function . 将每个对function调用的结果结合起来。 Ie, instead of mapping function alone, you map join . function 即,您可以映射join . function而不是单独使用映射function join . function join . function , which has type Char -> String . join . function ,其类型为Char -> String Mapping that has the desired type String -> [String] . 具有所需类型String -> [String]映射。

     lexi = map $ join . function 
  • Join the final result of the mapping, ie lexi = join . map function 加入映射的最终结果,即lexi = join . map function lexi = join . map function . lexi = join . map function This combination of mapping and joining the results is actually an extremely common task, it has a special operator: monadic bind! 映射和联接结果的这种组合实际上是一个极为常见的任务,它具有一个特殊的运算符:monadic bind!

     lexi x = x >>= function 

New version 新版本

So we know that 所以我们知道

words, lines :: String -> [String]

thus words . lines 这样的words . lines words . lines can not work, because you're trying to feed a list of strings into a function that only accepts a single string. words . lines无法工作,因为您正尝试将字符串列表提供给仅接受单个字符串的函数。 What you can of course do though is map words over the result, ie map words . lines 当然,您可以做的是在结果放置映射 words ,即map words . lines map words . lines . map words . lines That has in fact the correct signature and probably does just what you want. 实际上,它具有正确的签名,并且可能会按照您的要求进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM