“@"是什么意思?符号是指参考 Haskell 中的列表吗? [英] What does the "@" symbol mean in reference to lists in Haskell?

查看:20
本文介绍了“@"是什么意思?符号是指参考 Haskell 中的列表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一段如下所示的 Haskell 代码:

I've come across a piece of Haskell code that looks like this:

ps@(p:pt)

@ 符号在这种情况下是什么意思?我似乎在 Google 上找不到任何信息(遗憾的是很难在 Google 上搜索符号),而且我在 Prelude 文档中找不到该函数,所以我想它一定是某种语法糖.

What does the @ symbol mean in this context? I can't seem to find any info on Google (it's unfortunately hard to search for symbols on Google), and I can't find the function in the Prelude documentation, so I imagine it must be some sort of syntactic sugar instead.

推荐答案

是的,这只是语法糖,@ 朗读为as".ps@(p:pt) 为您提供

Yes, it's just syntactic sugar, with @ read aloud as "as". ps@(p:pt) gives you names for

  1. 列表:ps
  2. 列表的头部:p
  3. 列表的尾部:pt

如果没有 @,您必须在 (1) 或 (2):(3) 之间进行选择.

Without the @, you'd have to choose between (1) or (2):(3).

这个语法实际上适用于任何构造函数;如果您有 data Tree a = Tree a [Tree a],则 t@(Tree _ kids) 可让您访问树及其子树.

This syntax actually works for any constructor; if you have data Tree a = Tree a [Tree a], then t@(Tree _ kids) gives you access to both the tree and its children.

这篇关于“@"是什么意思?符号是指参考 Haskell 中的列表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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