免分:对放置括号的位置感到困惑 [英] Point-free: confused about where to put parenthesis

查看:86
本文介绍了免分:对放置括号的位置感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

let list_to_string = (String.concat "") (List.map (String.make 1));;

这是错误的,但是我如何理解仍然需要提供该参数?该参数应为char list类型,即首先需要应用的功能是(List.map (String.make 1)),然后将其传递给String.concat "".我想我已经尝试过所有可以想到的括号组合了...到目前为止还算不上快乐.

This is wrong, but how do I make it understand that the argument is still to be supplied? The argument is expected to be of type char list, I.e. the first function that needs to be applied to it is the (List.map (String.make 1)), and then pass it to String.concat "". I think I've tried all combinations of parenthesis I could think of... no joy so far.

帮助?

我也认为我可以这样做:

I also figured I could do it like this:

let ($) f g x = f (g x);;
let list_to_string = (String.concat "") $ (List.map (String.make 1));;

但是只是想确保没有更好的方法.

But just wanted to make sure there isn't a better way.

推荐答案

真正的(而且总是很困惑)问题是OCaml没有内置的函数组合运算符.因此,对于无点编码而言,开箱即用的效果不是很好.如果您真的想了解它,还需要flip,它可以反转两个参数函数的参数顺序.

The real (and always perplexing) problem is that OCaml doesn't have a built-in function composition operator. So it's not so good out of the box for pointfree coding. If you really want to get fancy with it, you also need flip, which inverts the order of the arguments of a two-argument function.

let flip f a b = f b a

无论如何,一旦将函数组成定义为$,我的解决方案就不会出现任何问题.您可以省略一些括号:

At any rate, I don't see any problem with your solution once you've defined function composition as $. You can leave out some of the parentheses:

# let lts = String.concat "" $ List.map (String.make 1);;
val lts : char list -> string = <fun>

关于效率,我认为这比实际的代码更令人困惑.否则,您应该使用Edwin建议的功能.

As to efficiency, I assume this is more of a puzzle than a practical bit of code. Otherwise you should use the functions that Edwin suggests.

这篇关于免分:对放置括号的位置感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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