将字符串拆分为Clean中的字符串列表 [英] Split string to a list of strings in Clean

查看:149
本文介绍了将字符串拆分为Clean中的字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于资源有限,我需要在这里提出一个问题 。我一直在努力的功能编程,无休止的Haskell教程并没有真正帮助我。所以我想在Clean语言中实现的是将一个像car cow cat这样的字符串分割成一个字符串列表 [car , 牛, 猫] 。你能否给我一个详细的答案(不一定是完整的代码),如何迭代这个字符串,特别是当新构造的字符串被添加到列表中时的部分?

解决方案

我将提供一个简单的解决方案。在Haskell中有更好的方法可以做到这一点,但是对于函数式编程中的新人来说,这是最简单的方法,不需要使用像takeWhile这样的Haskell函数,甚至是任何折叠和贴图...



你基本上想模拟迭代列表,所以这里是我的建议:
$ b


  1. 定义一个函数,它将采用一个字符串和一个拆分字符。这个函数将返回一个字符串列表 - spliton :: String - >字符 - >为了移动列表,我们需要吞噬角色,直到我们击中了我们的一个分裂角色。我们还希望保存迄今保存的单词以及整个单词列表。
    为此,我们将定义一个子函数来保存状态


    spliton':: String - >字符 - >字符串 - > [字符串] - > [String]



    spliton'[] _ sofar res = res ++ [sofar]



    我还包含了最简单的子句 - 一个空字符串。当我们的字符串为空时,我们只想返回到目前为止所保存的内容。

  2. 现在让我们继续讨论我们的实际递归函数:
    如果我们点击我们的分割字符,我们将添加到目前为止我们保存的字符串,并重新启动一个空的当前状态字符串
    如果我们没有点击分割字符,我们将添加字符转换为当前状态字符串

      spliton'(currchar:rest)splitby sofar res 
    | currchar == splitby = spliton'rest splitby(res ++ [sofar])
    |否则= spliton'休息splitby(sofar ++ [currchar])res



$ b $因此,总结我们的代码:

  spliton :: String  - >字符 - > [String] 
spliton source splitchar = spliton'source splitchar [] []

spliton':: String - >字符 - >字符串 - > [字符串] - > [String]
spliton'[] _ sofar res = res ++ [sofar]
spliton'(currchar:rest)splitby sofar res
| currchar == splitby = spliton'rest splitby(res ++ [sofar])
|否则= spliton'休息splitby(sofar ++ [currchar])res

注意:这不会摆脱空串 - 意思是如果你有很多多余的空格 - 你会让它们被添加到列表中。我会让你考虑如何处理这种情况 - 希望这可以帮助你开始。


Because of the limited amount of resources, I need to propose a question here. I have been struggling with functional programming, the endless Haskell tutorials don't really help me. So what I want to achieve, in Clean language, is to split a string like " car cow cat " to a list of strings ["car","cow","cat"]. Can you provide me a detailed answer (does not have to be complete code), on how to iterate through this string, and especially the part when the newly constructed strings are added to the list?

解决方案

I'm going to offer a simple solution. There are infinitely better ways of doing this in Haskell, but it's the simplest I can think for someone new in functional programming, without using any specifically Haskell function like takeWhile, or even any folds and maps...

You basically want to simulate iterating over a list, so here is what I suggest:

  1. Define a function that will take a string and a split-by character. This function will return a list of strings - spliton :: String -> Char -> [String]

  2. To move over the list, we'll want to gobble up characters until we hit one of our splitting characters. We'll also want to save the word we've saved up until now, and the entire list of words. For that, we'll define a subfunction that will save the states

    spliton' :: String -> Char -> String -> [String] -> [String]

    spliton' [] _ sofar res = res ++ [sofar]

    I've also included the simplest clause - an empty string. When our string is empty, we'll just want to return what we have saved so far.

  3. Now lets move on to our actual recursive function: If we hit our split character, we'll add the string we have saved so far to the list and restart with an empty current-state string If we don't hit the split character, we'll add the character to the current-state string

    spliton' (currchar:rest) splitby sofar res
         | currchar==splitby = spliton' rest splitby "" (res++[sofar])
         | otherwise = spliton' rest splitby (sofar++[currchar]) res
    

So, to summarize our code:

spliton :: String -> Char -> [String]
spliton source splitchar = spliton' source splitchar [] []

spliton' :: String -> Char -> String -> [String] -> [String]
spliton' [] _ sofar res = res ++ [sofar]
spliton' (currchar:rest) splitby sofar res
         | currchar==splitby = spliton' rest splitby "" (res++[sofar])
         | otherwise = spliton' rest splitby (sofar++[currchar]) res

Note: This will not however get rid of the empty string - meaning if you have many superfluous spaces - you'll get them added to the list. I'll leave you to think how to handle that case - hope this can help you get started.

这篇关于将字符串拆分为Clean中的字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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