Haskell罕见模式匹配 [英] Haskell rare pattern matching

查看:131
本文介绍了Haskell罕见模式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  s @Hello=我的好朋友曾向我展示过罕见的模式匹配行为。 s 

结果是:

 > s 
>

它开始打印字符串,但永远不会结束,什么是布莱恩?

解决方案

let s和顶级表达式在 = 左边的所有内容都在右边,所以你创建了一个循环的底部值得注意。



请注意,它的行为方式是相同的:

  Prelude>让s =(s :: String)
Prelude> s

这是因为(简化) print 在 String 上的定义相当于:

  printString chars = putChar''>> mapM_ putChar chars 

因为 chars 是a循环显示 mapM_ putChar字符挂起。


A good friend of mine once showed to me this rare behavior of pattern matching in haskell

s@"Hello" = s 

The result is:

>s
>"

It begins to print the String but never ends, what is goin on?

解决方案

In lets and top level expressions everything on the left-hand side of the = is in scope on the right-hand side. So you've created a looping "bottom" value.

Notice this behaves the same way:

Prelude> let s = (s::String)
Prelude> s
"

That's because (simplifying) print on String is defined something equivalent to:

printString chars = putChar '"' >> mapM_ putChar chars

Because chars is a loop the mapM_ putChar chars appears to hang.

这篇关于Haskell罕见模式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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