在Haskell中的条件析取

huangapple go评论56阅读模式
英文:

Conditioned disjunction in Haskell

问题

这是你要翻译的部分:

"编写一个函数,该函数以一个布尔值列表作为输入,其中元素的数量是三的倍数,并返回通过顺序应用“条件析取”操作到列表元素的第k个三元组(第k个三元组是具有编号k、k+1和k+2的三个元素,其中k = 1,3,...,n-2)而获得的值。将操作应用于第k对元素的结果将成为中间列表的第(k+2)个元素。给出三个使用该函数的示例。"

以下是你提供的Haskell代码:

     calc::[Bool]->Bool
      calc [] = False
      calc (p:[]) = False
      calc (p:q:[]) = False
      calc (p:q:r:[]) = ((not q||p)&&(q||r)) 
      calc (p:q:r:xs) = calc(((not q||p)&&(q||r)):r:xs)

编译器报告以下错误:

"21.hs:3:19: parse error on input ‘=’"

如果需要进一步帮助,请提出具体问题。

英文:

> Write a function that takes as input a list of Boolean values, the number of elements in which is a multiple of three, and returns a value that is obtained by sequentially applying the operation "conditional disjunction" to the k-th triple of list elements (the k-th triple is a triple of elements with numbers k, k+1 and k+2, k = 1,3,...,n-2). The result of applying the operation to the k-th pair of elements becomes the (k+2)th element of the intermediate list. Give three examples of using the function.


I have tried the following, but it doesn't work.

     calc::[Bool]->Bool
      calc [] = False
      calc (p:[]) = False
      calc (p:q:[]) = False
      calc (p:q:r:[]) = ((not q||p)&&(q||r)) 
      calc (p:q:r:xs) = calc(((not q||p)&&(q||r)):r:xs)

The compiller says the following.

21.hs:3:19: parse error on input ‘=’

答案1

得分: 3

你的缩进有问题 - 你为你的模式编写了单独的函数实现,所以它们每个都应该从第0列开始:

calc::[Bool]->Bool
calc [] = False
calc (p:[]) = False
calc (p:q:[]) = False
calc (p:q:r:[]) = ((not q||p)&& (q||r)) 
calc (p:q:r:xs) = calc(((not q||p)&& (q||r)):r:xs)

参见Learn you a Haskell: Syntax in functions

英文:

Your indentation is wrong - you've got separate function implementations for your patterns, so each of them should start on column 0:

calc::[Bool]->Bool
calc [] = False
calc (p:[]) = False
calc (p:q:[]) = False
calc (p:q:r:[]) = ((not q||p)&&(q||r)) 
calc (p:q:r:xs) = calc(((not q||p)&&(q||r)):r:xs)

See Learn you a Haskell: Syntax in functions

huangapple
  • 本文由 发表于 2023年6月8日 19:16:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定