Haskell:带有匿名/ lambda函数的函数组合 [英] Haskell: function composition with anonymous/lambda function

查看:378
本文介绍了Haskell:带有匿名/ lambda函数的函数组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  t1 =(reverse)在学习功能性编程考试时,我遇到了以前的测试中的以下问题: 。2。words。\ _  - > name)! 

任务是编写语句的输出。变量名称是以Smith,John的形式编写的学生姓名。如果我将声明输入到WinHugs中,我会得到以下输出:

  [John,Smith,] 

我明白什么是函数反转,运营商连接它们。我不明白这里发生了什么:

  \ _  - >名称

什么是斜杠,下划线和箭头?另外,引号中的惊叹号是做什么的? (没有?)

解决方案

这是一个lambda函数,它放弃其(仅)参数(即!)并产生 name



作为另一个lambda例子,下面是一个lambda函数,它的参数是正方形:

  \x  - > x * x 

\

_ 表示关于我们不关心其名称的变量。


$ b

- > 将lambda函数的参数与用于指定结果的表达式分开。


While studying for a Functional Programming exam, I came across the following question from a previous test:

t1 = (reverse . take 2 . words . \ _ -> name)"!"

The task is to write the output of the statement. The variable name refers to the student's name, written in the form "Smith, John". If I enter the statement into WinHugs, I get the following output:

["John","Smith,"]

I understand what the functions reverse, take and words are doing and I understand how the . operator connects them. What I don't understand is what is happening here:

\ _ -> name

What are the slash, underscore and "arrow" for? Also, what does the exclamation point in quotation marks do? (nothing?)

解决方案

It's a lambda function that discards its (only) argument (i.e. "!") and yields name.

As another lambda example, the following would be a lambda function that squares its argument:

\x -> x * x

The \ is the notation used to introduce a lambda function.

The _ means "variable about whose name we do not care".

The -> separates the lambda function's arguments from the expression used to specify its result.

这篇关于Haskell:带有匿名/ lambda函数的函数组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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