如何进行咖喱工作? [英] How does currying work?

查看:112
本文介绍了如何进行咖喱工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Haskell和FP一般都很陌生。我已经阅读了许多描述柯里化的着作,但我还没有找到解释它如何工作的解释。



这是一个函数:(+):: a - > (a - > a)
如果我做了(+)4 7 ,那么函数需要 4 并返回一个函数,它取得 7 并返回 11 。但是 4 会发生什么?第一个函数对 4 有什么作用? (a - > a) 7 ?



当我考虑一个更复杂的函数时,情况会变得更加混乱:

  max':: Int  - > (Int  - > Int)
max'm n | m> n = m
|否则= n

(Int - > Int)比较它的参数?它只需要一个参数,但需要两个才能完成 m>

解决方案



  max'= \m  - > \\\
- >如果(m> n)
然后m
else n

lambda表达式。 max'是一个lambda的值,它本身返回一个给定m的lambda,它返回值。



因此max'4是

  max'4 = \\\
- >如果(4> n)
则4
else n


I'm very new to Haskell and FP in general. I've read many of the writings that describe what currying is, but I haven't found an explanation to how it actually works.

Here is a function: (+) :: a -> (a -> a) If I do (+) 4 7, the function takes 4 and returns a function that takes 7 and returns 11. But what happens to 4 ? What does that first function do with 4? What does (a -> a) do with 7?

Things get more confusing when I think about a more complicated function:

max' :: Int -> (Int -> Int)
max' m n | m > n = m
         | otherwise = n

what does (Int -> Int) compare its parameter to? It only takes one parameter, but it needs two to do m > n.

解决方案

Does this help?

max' = \m -> \n -> if (m > n)
                       then m
                       else n

Written as lambdas. max' is a value of a lambda that itself returns a lambda given some m, which returns the value.

Hence max' 4 is

max' 4 = \n -> if (4 > n)
                   then 4
                   else n

这篇关于如何进行咖喱工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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