'在Haskell函数名中的含义是什么? [英] The meaning of ' in Haskell function name?

查看:92
本文介绍了'在Haskell函数名中的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是引用'用于?我已经阅读了咖喱函数,并阅读了两种定义添加函数的方法 - curried和uncurried。咖喱版......

What is quote ' used for? I have read about curried functions and read two ways of defining the add function - curried and uncurried. The curried version...

myadd' :: Int -> Int -> Int
myadd' x y = x + y

...但是,报价单。那么,'

...but it works equally well without the quote. So what is the point of the '?

推荐答案

没有任何Haskell。它只是该函数名称的一部分。

The quote means nothing to Haskell. It is just part of the name of that function.

人们倾向于将它用于内部函数。如果你有一个使用累加器参数累加列表的函数,那么你的sum函数将带有两个参数。这很丑陋,所以你需要两个参数的 sum'函数和一个参数的 sum 函数,比如 sum list = sum'0 list

People tend to use this for "internal" functions. If you have a function that sums a list by using an accumulator argument, your sum function will take two args. This is ugly, so you make a sum' function of two args, and a sum function of one arg like sum list = sum' 0 list.

编辑,也许我应该只显示代码:

Edit, perhaps I should just show the code:

sum' s [] = s
sum' s (x:xs) = sum' (s + x) xs

sum xs = sum' 0 xs

您这样做是为了使 sum'是尾递归的,所以public API很好看。

You do this so that sum' is tail-recursive, and so that the "public API" is nice looking.

这篇关于'在Haskell函数名中的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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