【问题标题】:Pattern match on chars字符上的模式匹配
【发布时间】:2013-10-07 20:35:50
【问题描述】:

我对 Elm (elm-server 0.9.2) 很陌生,我遇到了一个对我来说已经成为障碍的问题。

这是我的问题:

根据version-0.9 文档我应该可以写:

stripCommas str =
  case str of
    ',' :: rest -> stripCommas rest
    c   :: rest ->  c  :: stripCommas rest

所以为了测试这个我基本上做了我自己的功能(非常相似:)):

stripNewLine str = 
  case str of
   '\n' :: rest -> stripNewLine rest
    c   :: rest ->  c  :: stripNewLine rest

但他们的展位失败了,经过一些调试后我在 javascript 中注意到了这一点:

var stripNewLine = function(str){
    return function(){
      switch (str.ctor) {
        case '::':
          switch (str._0) {
            case Chr '\n':
              return stripNewLine(str._1);
          }
          return _L.Cons(str._0,stripNewLine(str._1));
      }_E.Case($moduleName,'between lines 22 and 33')}();};

我对 javascript 不太了解,但似乎 Chr '\n' 应该是 Chr('\n'),我可能错了...有人可以在这里指出我正确的方向吗?因为我迷路了...

【问题讨论】:

    标签: frp elm


    【解决方案1】:

    这是一个 Elm 错误 - 自最新稳定版本以来已修复 - 你是对的,它与错误生成的 Javascript 有关。

    此外,您从该公告博客文章中复制的示例代码中还有一个逻辑问题,即它正在执行非详尽的模式匹配。

    字符串是字符列表(即String 只是一个[Char]),因此正确的模式匹配应该处理空列表的情况,即:

    stripCommas str =
      case str of
        []          -> str
        ',' :: rest -> stripCommas rest
        c   :: rest ->  c  :: stripCommas rest
    
    main = asText <| stripCommas "1,2,3,4,5"
    

    你可以测试这个here(从版本选项中选择“master/HEAD”,这是比当前版本更新的版本,存在JS生成错误)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-13
      • 1970-01-01
      • 2019-03-19
      • 2018-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多