简体   繁体   English

在Haskell中接受或返回函数的函数语法

[英]Syntax for functions that take in or return functions in Haskell

I'm learning Haskell via Learn You a Haskell and I'm pretty sure he went over this at some point but I can't figure out where. 我正在通过“了解您的Haskell”来学习Haskell,我很确定他在某个时候已经解决了这个问题,但我不知道在哪里。 Say I want to have a function return a pair, list of pairs, or general tuple. 说我想让一个函数返回一对,对列表或常规元组。 How is the type signature for this written? 对此的类型签名是如何写的? For example, for some given even number I want to return the list of unique pairs of primes that add up to it. 例如,对于某些给定的偶数,我想返回加起来的唯一素数对的列表。 I have a function isPrime that tells whether or not an Int is a prime. 我有一个函数isPrime ,它告诉一个Int是否是素数。

pairs :: Int -> [(Int a, Int b)]
pairs n
  | n < 4 = []
  | odd n = []
  | otherwise = [(a, b) | a < b, isPrime a, isPrime b, a+b == 1]

When I try to compile this, I get the error saying "Int has too few arguments." 当我尝试对此进行编译时,出现错误消息“ Int的参数太少”。 So I tried the following syntax instead: 因此,我尝试使用以下语法:

pairs :: Int -> [((Int a), (Int b))]

But I got the same error. 但是我遇到了同样的错误。 So in general how does typesetting functions involving pairs work? 因此,通常涉及对的排版功能如何工作?

Your problem isn't one of formatting. 您的问题不是格式化之一。 Int a and Int b are ill-kinded. Int aInt b类型不正确。 Int can't be applied because it isn't a (type-level) function. Int不是(类型级别)函数,因此无法应用。 You just want Int -> [(Int, Int)] . 您只需要Int -> [(Int, Int)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM