标签: currying

编写适当的自定义读取实例

哈斯凯勒同学们大家好,

\n\n

我现在正在学习 Haskell 一个月,并且正在努力为个人数据类型创建自定义读取实例。

\n\n

我遵循了这一点以及Learn Yourself a Haskell 中的相关章节,这是我的代码片段。

\n\n
data Position      =  Position (Absc,Ordn) deriving (Show)\ninstance Read (Position) where\nreadsPrec _ input =\n    let absc = List.filter (/=\'(\') $ takeWhile (/=\',\')\n        ordn = List.filter (/=\')\') $ tail (dropWhile (/=\',\') )\n    in (\\str -> Position ( (read (absc str) :: Int)\n                       , (read (ordn str) :: Int) ) ) input\ntype Absc = Int\ntype Ordn = Int\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的目标是解析输入"(1,3)"以输出类似的内容Position (1,3)

\n\n

但是,我收到以下错误消息:

\n\n …

haskell currying typeclass deriving

1
推荐指数
1
解决办法
766
查看次数

如何在 Typescript 4 中编写咖喱和撰写?

在经历了可变参数类型之后,我想做这个,但我想知道如何使用函数数组。

这是我的第一次尝试:

function curry<T extends any[]>(fn: (...args: T) => any) {
  return function(...args: T) {
    return args.length >= fn.length
      ? fn(...args)
      : curry(fn.bind(undefined, ...args));
  }
}
Run Code Online (Sandbox Code Playgroud)

但是因为fn.bind我得到“'(...args: T) => any' 类型的'this' 上下文不能分配给'(this: undefined, ...args: any[ ]) => 任何'。”

有任何想法吗?

functional-programming currying composition typescript

1
推荐指数
1
解决办法
1006
查看次数

通过函子和函数组合使用 Haskell 进行柯里化

我有以下问题。我有一个功能很好用。

\n
getName :: Style -> Css -> Maybe String\ngetName s css = map intToChar .  intToBase 26 <$> getIndex s css\n
Run Code Online (Sandbox Code Playgroud)\n

我想知道为什么我不能这样定义它:

\n
getName :: Style -> Css -> Maybe String\ngetName = map intToChar .  intToBase 26 <$> getIndex\n
Run Code Online (Sandbox Code Playgroud)\n

我认为柯里化可以让我将其作为带有两个参数的函数返回,而不是作为具有两个参数的函数

\n

如果它有助于您的理解:

\n
intToChar :: Int -> Char\nintToBase :: Int -> Int -> [Int]\ngetIndex :: Style > Css -> Maybe Int\n
Run Code Online (Sandbox Code Playgroud)\n

啊,不要忘记错误:

\n
error:\n    \xe2\x80\xa2 Couldn't match type \xe2\x80\x98[a] -> Maybe Int\xe2\x80\x99 with \xe2\x80\x98Int\xe2\x80\x99\n      Expected type: …
Run Code Online (Sandbox Code Playgroud)

haskell currying

1
推荐指数
1
解决办法
119
查看次数

Haskell中的函数curry

我有一个功能:

powerOf :: Int -> Int -> Int
Run Code Online (Sandbox Code Playgroud)

示例os用法:

*Main Data.List> powerOf 100 2
2
*Main Data.List> powerOf 100 5
2
Run Code Online (Sandbox Code Playgroud)

我有两个问题.首先 - 为什么它不起作用:

map (powerOf 100) [2, 5]
Run Code Online (Sandbox Code Playgroud)

我想得到[2,2].

第二个问题.我试图创建pariatl函数.像这样的东西:

powerOfN :: Int -> Int
powerOfN num = powerOf num
Run Code Online (Sandbox Code Playgroud)

使用它这样的方式:

let powerOf100 = powerOfN 100
powerOf100 2
powerOf100 5
Run Code Online (Sandbox Code Playgroud)

但我收到了错误消息:

simplifier.hs:31:15:
    Couldn't match expected type `Int'
           against inferred type `Int -> Int'
    In the expression: powerOf num
    In the definition of `powerOfN': powerOfN num = powerOf num
Run Code Online (Sandbox Code Playgroud)

这里有很多代码: …

haskell currying partial-application

0
推荐指数
1
解决办法
367
查看次数

咖喱所有swift函数参数,但不要调用该函数

我有以下快速功能:

func foo(bar: String)(_ baz: String) {
  NSLog("bar \(bar), baz: \(baz)")
}

let f = foo("fizz")("buzz") // won't compile, foo returns Void
Run Code Online (Sandbox Code Playgroud)

我想把它传递给dispatch_async,但我不能,因为我不能在不调用函数的情况下讨论这两个参数.我如何咖喱barbaz没有打电话foo

currying swift

0
推荐指数
1
解决办法
165
查看次数

在scala中curry有用的场景是什么?

我找到了一些关于什么是咖喱以及它能做些什么的好帖子.它可以将带有参数列表的函数转换为函数列表.我不清楚它在什么情况下是有用的.谁能给我一个具体的例子?

Scala currying与部分应用的函数

scala currying

0
推荐指数
1
解决办法
120
查看次数

函数类型签名中的右关联性

我很难理解Haskell中类型签名背后的原因.

1)->据说是右关联的,是否意味着它可以用类似的方式理解,例如4 ^(2 ^(3 ^ 2))?

2)使用简单函数的类型签名,以表达我的疑惑(来解释我的理解是,我会用a,b,c来代替Num a => a的或Int的):

myAdd :: a -> b -> c
myAdd x y = x+y
Run Code Online (Sandbox Code Playgroud)

这意味着函数接受参数a并返回b最终返回的函数c

但它可以重写为:

myAdd :: (a->(b->c))
Run Code Online (Sandbox Code Playgroud)

由于大多数学习材料都表明c在我们的例子中是函数myAdd的结果,为什么根据括号的使用它表明第一个'操作'是b->c?如何从该类型签名推断出执行操作的顺序?

3)我被赋予了执行任务

map f xs 
Run Code Online (Sandbox Code Playgroud)

使用 foldr,(.)(:)导致:

map f xs = foldr ((:) . f) [] xs
Run Code Online (Sandbox Code Playgroud)

我对理解上述功能的工作没有任何问题,但我们再来一次 - 键入签名.如果我们假设,这名都是统一的这样类型的a代表,在所有合约相同类型的,现在看来,这cd在来表示a …

haskell types functional-programming currying type-signature

0
推荐指数
1
解决办法
450
查看次数

使用n个参数在javascript中进行函数的currying

如果f ::(a,b) - > c,我们可以定义curry(f)如下:

咖喱(f)::((a,b) - > c) - > a - > b - > c

const curry = f => a => b => f(a, b);
const sum = curry((num1, num2) => num1 + num2);
console.log(sum(2)(3)); //5
Run Code Online (Sandbox Code Playgroud)

我们如何实现带有n个参数的函数的通用咖喱函数?

javascript functional-programming currying curry

0
推荐指数
1
解决办法
976
查看次数

这个haskell双功能组合的类型是什么?

t2 = (\x y z-> x.y.x)

GHCI告诉我这个:

t2 :: (b1 -> b2) -> (b2 -> b1) -> p -> b1 -> b2

我无法理解这种类型的签名是如何形成的.到目前为止,我已经认为r-most-x基本上是一个函数,它接受a b2并返回a b1,那么这b1是中间函数的输入y,并b2再次输出?它不应该返回新类型的值b3或什么?

haskell lambda-calculus currying function-composition curry

0
推荐指数
1
解决办法
93
查看次数

Typescript返回值或curried函数

鉴于以下Typescript代码,我收到一个错误

TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'AddReturnType' has no compatible call signatures.

为什么不能AddReturnType使用该呼叫?

type AddReturnType = number | ((arg0: number) => number);
function add(x: number, y?: number) : AddReturnType {
    if (!y) {
        return (val) => val + y;
    }
    return x + y;
}

add(1)(2);
Run Code Online (Sandbox Code Playgroud)

javascript currying typescript

0
推荐指数
1
解决办法
36
查看次数