理解 Haskell as-pattern [英] Understanding the Haskell as-pattern

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

问题描述

我正在阅读 Real World Haskell,并试图理解 as-pattern.

I'm reading through Real World Haskell, and am trying to understand the as-pattern.

来自书中(第 4 章):

From the book (Chapter 4):

suffixes :: [a] -> [[a]]
suffixes xs@(_:xs') = xs : suffixes xs'
suffixes _ = []

这本书解释了@符号,

"...将变量 xs 绑定到匹配右侧的值@ 符号."

"...bind the variable xs to the value that matches the right side of the @ symbol."

我无法理解这个解释.假设我打电话

I'm having trouble understanding this explanation. Supposing I call

suffixes "hello"

明确地,上面带有@ 的行对此(在第一次迭代中)会做什么?我知道函数的结果是什么,但看不出我们是如何从上面的代码中得到的.

Explicitly, what would the above line with the @ do to this (on the first iteration)? I know what the result of the function is, but cannot see how we get there from the above code.

推荐答案

xs' 将绑定到字符串 "ello".

xs 将绑定到字符串 "hello".

@ 模式允许您为变量命名,同时匹配其结构并可能为组件命名.

The @ pattern lets you give a name to a variable while also matching its structure and possibly giving name to the components.

这篇关于理解 Haskell as-pattern的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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