haskell中给定范围内所有偶数的平方列表

haskell中给定范围内所有偶数的平方列表,haskell,recursion,Haskell,Recursion,我需要返回给定范围内所有偶数的平方列表。所以函数是“平方低-高”。输入和输出的示例是平方16=[4,16,36]。到目前为止,我认为我的逻辑是正确的,但由于我对Haskell非常陌生,我不确定这里出了什么问题 Squares low high = let aux xxs low high | ([rem low 2] /= 0) = aux xxs (1 + low) high | ([rem low 2]==0) = aux ([

我需要返回给定范围内所有偶数的平方列表。所以函数是“平方低-高”。输入和输出的示例是平方16=[4,16,36]。到目前为止,我认为我的逻辑是正确的,但由于我对Haskell非常陌生,我不确定这里出了什么问题

    Squares low high = 
         let aux xxs low high
         | ([rem low 2] /= 0) = aux xxs (1 + low) high
         | ([rem low 2]==0) = aux ([low * low] ++ xxs) (1  + low) high
         | otherwise = xxs
         in aux [] low high
任何帮助都将不胜感激

Squares low high = -- ...
这一行已经中断,因为只有类型/模块可以以大写字母开头

 let aux xxs low high
 | ([rem low 2] /= 0) = aux xxs (1 + low) high
 | ([rem low 2]==0) = aux ([low * low] ++ xxs) (1  + low) high
 | otherwise = xxs
调用
aux
需要多长时间?只有在点击
时才停止递归,否则
。但是整数不是偶数就是奇数,因此您永远不会返回
xss
。相反,您需要检查
low>high

请注意,您可以在不使用辅助功能的情况下定义
正方形

squares low high
  | low > high  = []
  | even low    = low*low : squares (low + 1) high
  | otherwise   = squares (low + 1) high
或者使用
映射
过滤器

squares low high = map (^2) . filter even $ [low..high]
squares low high = [x^2| x <- [low..high], even x]
squares low high = map (\x -> x * x) . filter even $ [low .. high]
或者列表理解(这基本上是
map
filter
的语法糖):

squares low high=[x^2 | x
这一行已经中断,因为只有类型/模块可以以大写字母开头

 let aux xxs low high
 | ([rem low 2] /= 0) = aux xxs (1 + low) high
 | ([rem low 2]==0) = aux ([low * low] ++ xxs) (1  + low) high
 | otherwise = xxs
调用
aux
需要多长时间?只有在单击
时才会停止递归,否则
。但是整数是偶数或奇数,因此永远不会返回
xss
。相反,您需要检查
低>高

请注意,您可以在不使用辅助功能的情况下定义
正方形

squares low high
  | low > high  = []
  | even low    = low*low : squares (low + 1) high
  | otherwise   = squares (low + 1) high
或者使用
映射
过滤器

squares low high = map (^2) . filter even $ [low..high]
squares low high = [x^2| x <- [low..high], even x]
squares low high = map (\x -> x * x) . filter even $ [low .. high]
或者列表理解(这基本上是
map
filter
的语法糖):


squares low high=[x^2 | x看起来您正在尝试执行以下操作:

squares :: Int -> Int -> [Int]
squares low high = aux [] low high
    where aux xxs low high
        | low > high = xxs
        | rem low 2 /= 0 = aux xxs (1 + low) high
        | otherwise = aux (xxs ++ [low * low]) (1  + low) high
您的保护错误有几个原因。首先,
[rem low 2]
[Int]
而不是
Int
,因此您需要删除括号。其次,您的
否则
子句将永远无法到达,因为前两种情况之一必须为真。您应该将终止检查移到开头

您还以错误的顺序构建输出列表-如果希望下一个值位于末尾,则在添加新元素时应使用
xxs++[low*low]
。请注意,使用
++
构建列表效率低下,最好使用
(:)
从前面向列表添加元素

最后,您可以使用
map
filter
以一种更简单的方式执行此操作:

squares low high = map (^2) . filter even $ [low..high]
squares low high = [x^2| x <- [low..high], even x]
squares low high = map (\x -> x * x) . filter even $ [low .. high]
或列表:

squares low high = [x * x | x <- [low .. high], even x]

squares low high=[x*x | x看起来您正在尝试执行以下操作:

squares :: Int -> Int -> [Int]
squares low high = aux [] low high
    where aux xxs low high
        | low > high = xxs
        | rem low 2 /= 0 = aux xxs (1 + low) high
        | otherwise = aux (xxs ++ [low * low]) (1  + low) high
您的保护错误有几个原因。首先,
[rem low 2]
[Int]
而不是
Int
,因此您需要删除括号。其次,您的
否则
子句将永远无法到达,因为前两种情况之一必须为真。您应该将终止检查移到开头

您还以错误的顺序构建输出列表-如果希望下一个值位于末尾,则在添加新元素时应使用
xxs++[low*low]
。请注意,使用
++
构建列表效率低下,最好使用
(:)
从前面向列表添加元素

最后,您可以使用
map
filter
以一种更简单的方式执行此操作:

squares low high = map (^2) . filter even $ [low..high]
squares low high = [x^2| x <- [low..high], even x]
squares low high = map (\x -> x * x) . filter even $ [low .. high]
或列表:

squares low high = [x * x | x <- [low .. high], even x]

squares low-high=[x*x | x从
low
high
生成数字:

gen a b = a : gen (a+1) b
对吗?…不,不对:它不会停止。我们可以添加停止条件吗

gen a b | .....     = []
        | otherwise = a : gen (a+1) b
如果我们只想要偶数呢

gen a b | .....            = []
        | rem ... ... == 0 = a : gen (a+1) b
        | otherwise        =     .....
调整它来生成正方形很容易。但是我们真的需要测试我们自己生成的数字吗?如果
a
是偶数呢

gen a b | even a = g a 
  where 
    g a | .....     = []
        | otherwise = a : g (a+2)    -- only evens are generated

如果是奇数呢?。

生成从
的数字:

gen a b = a : gen (a+1) b
对吗?…不,不对:它不会停止。我们可以添加停止条件吗

gen a b | .....     = []
        | otherwise = a : gen (a+1) b
如果我们只想要偶数呢

gen a b | .....            = []
        | rem ... ... == 0 = a : gen (a+1) b
        | otherwise        =     .....
调整它来生成正方形很容易。但是我们真的需要测试我们自己生成的数字吗?如果
a
是偶数呢

gen a b | even a = g a 
  where 
    g a | .....     = []
        | otherwise = a : g (a+2)    -- only evens are generated

如果是奇数呢?

去掉
rem low 2
周围的括号。另外,有三种情况:等于0,不等于0,和…”否则“在最后一个案例中,你希望看到什么?也许你需要检查<代码>低<高>代码>。我会考虑把它改写为列表理解。你可以在这里阅读列表理解:我认为其他情况只是在前两个案例不起作用时返回列表。无论如何,我把它变成了(低=高)。案例和I删除了REM低2的括号,现在它进入一个无限的循环,你已经考虑剩下的数除以2。你可以考虑舍入奇数<代码>低 UP和奇数<代码>高< /代码>下,可以在没有分支的情况下完成。确认rem low 2的位置。另外,您有三种情况:等于0、不等于0和……“否则”在最后一个案例中,你希望看到什么?也许你需要检查<代码>低<高>代码>。我会考虑把它改写为列表理解。你可以在这里阅读列表理解:我认为其他情况只是在前两个案例不起作用时返回列表。无论如何,我把它变成了(低=高)。案例和我删除了括号从REM低2,现在它进入一个无限的循环,你已经考虑剩余的数字除以2。你可以认为舍入奇数<代码>低< /代码> Up和奇数代码>高< /代码>下可以做没有分支的其余部分。o测试每个x是否为偶数。只需上下取整:
squares low high=[4*x*x | x无需测试每个x是否为偶数。只需上下取整:
squares low high=