I have a bit of homework to do and I am a complete newbie to Haskell. The question I am having trouble with is to write a function which when given an integer x
and a list of integers apply (x-y)*(x-y)
to each element in the list and output the new list, with y
being each element of the input list.
我有一些家庭作业要做,我对哈斯卡尔完全是个新手。我遇到的问题是编写一个函数,当给定一个整数x和一个整数列表时,它会将(x-y)*(x-y)应用到列表中的每个元素,并输出新的列表,y是输入列表的每个元素。
I have a very rough idea I will have to use the map
function but I'm unsure how to go about it.
我有一个很粗略的想法,我将不得不使用map函数,但是我不确定该怎么做。
I have been looking at examples for squaring each element in a list and kind of understand how that works, but how I would implement the (x-y)*(x-y)
with y
being the current element completely baffles me.
我一直在寻找将列表中的每个元素平方的例子,并大致了解它是如何工作的,但是我如何实现(x-y)*(x-y)以y为当前元素完全令我困惑。
squares :: [Int] -> [Int]squares (x:xs) = x * x : squares xssquares [] = []
the exact question I have been set is,
我设定的确切问题是,
Write a function
rela
which takes as arguments an integerx
and a list of integers. It returns a similar list, but where each elementy
has been replaced by(x-y)*(x-y)
, e.g.编写一个函数rela,它接受整数x和整数列表作为参数。它返回一个类似的列表,但是每个元素y都被(x-y)*(x-y)替换,例如。
Main> rela 2 [3,5,7][1,9,25]
I have managed to get it working after reading through some books, but the code I have made misses out the first element in the list. Any explanation why?
在读完一些书之后,我已经设法让它工作了,但是我所写的代码漏掉了列表中的第一个元素。任何解释为什么?
equation1 :: Int -> Int -> Intequation1 x y = (x-y)*(x-y)rela :: Int -> [Int] -> [Int]rela x [] =[]rela x (y:ys) = [ equation1 x y | y <- ys ]
4 个解决方案
#1
10
First of all, you should probably create a separate function that does what you want.
首先,您可能应该创建一个独立的函数来执行您想要的操作。
e.g.
如。
f x y = (x-y)*(x-y)
Now, every time you create a function in Haskell with multiple parameters, it actually "curries" the function, which means that you get a new function when you apply the first argument to it.
现在,每当你在Haskell中创建一个具有多个参数的函数时,它实际上会“凝结”这个函数,这意味着当你对它应用第一个参数时,你会得到一个新的函数。
So, you would get a new function by doing this
这样就得到了一个新的函数
g = f 5
The expression f 5
is actually a function
表达式f5实际上是一个函数
And you can apply a number to 'g' and x will always be '5'
你可以给g加上一个数字x总是5
So if we want to create a function that takes two parameters, 'x' and 'y', and applies (x-y)*(x-y)
to a list where y is the current element, then all we need to do is the following:
因此,如果我们想要创建一个包含两个参数的函数,即'x'和'y',并将(x-y)*(x-y)应用到一个以y为当前元素的列表中,那么我们需要做的就是:
f x y = (x-y)*(x-y)squareDifference x = map (f x) [1,2,3,4]
Which you can use by calling squareDifference 5
or any other number as an argument
你可以通过调用squareDifference 5或任何其他数字作为参数来使用它吗
A more general version would allow you to pass in a list as well
一个更通用的版本也可以让你传递一个列表
squareDifference x xs = map (f x) xs
Which you would call by doing squareDifference 3 [1,2,3]
用squareDifference 3 [1,2,3]
#2
10
do you understand lambda functions?
你理解函数吗?
map (\val -> the function) xs
is what you need.
是你需要的。
currying is even better, but not as simple.
咖喱更好,但没那么简单。
edit:
编辑:
more conceptual...
更多的概念……
map iterates down a list applying a function.
map迭代一个应用函数的列表。
map (+ 3) xs
uses the currying technique mentioned above. you could also:
使用上面提到的当前技术。你也可以:
map (\x -> x + 3) xs
to accomplish the same thing.
为了完成同样的事情。
#3
2
Simple example:
简单的例子:
rela :: Int -> [Int] -> [Int]rela x = map (\y -> (x-y)*(x-y))
Or might you want any perversions? -) Here you are with Applicatives:
或者你想要任何变态?-)这是您的申请表:
import Control.Applicativerela :: Int -> [Int] -> [Int]rela x = map $ (*) <$> (x-) <*> (x-)
#4
0
Hello I guess you mean this:
你好,我猜你的意思是:
Prelude> let rela n = map (\ x -> (x - n)^2)Prelude> rela 2 [3,5,7] [1,9,25]
#1
10
First of all, you should probably create a separate function that does what you want.
首先,您可能应该创建一个独立的函数来执行您想要的操作。
e.g.
如。
f x y = (x-y)*(x-y)
Now, every time you create a function in Haskell with multiple parameters, it actually "curries" the function, which means that you get a new function when you apply the first argument to it.
现在,每当你在Haskell中创建一个具有多个参数的函数时,它实际上会“凝结”这个函数,这意味着当你对它应用第一个参数时,你会得到一个新的函数。
So, you would get a new function by doing this
这样就得到了一个新的函数
g = f 5
The expression f 5
is actually a function
表达式f5实际上是一个函数
And you can apply a number to 'g' and x will always be '5'
你可以给g加上一个数字x总是5
So if we want to create a function that takes two parameters, 'x' and 'y', and applies (x-y)*(x-y)
to a list where y is the current element, then all we need to do is the following:
因此,如果我们想要创建一个包含两个参数的函数,即'x'和'y',并将(x-y)*(x-y)应用到一个以y为当前元素的列表中,那么我们需要做的就是:
f x y = (x-y)*(x-y)squareDifference x = map (f x) [1,2,3,4]
Which you can use by calling squareDifference 5
or any other number as an argument
你可以通过调用squareDifference 5或任何其他数字作为参数来使用它吗
A more general version would allow you to pass in a list as well
一个更通用的版本也可以让你传递一个列表
squareDifference x xs = map (f x) xs
Which you would call by doing squareDifference 3 [1,2,3]
用squareDifference 3 [1,2,3]
#2
10
do you understand lambda functions?
你理解函数吗?
map (\val -> the function) xs
is what you need.
是你需要的。
currying is even better, but not as simple.
咖喱更好,但没那么简单。
edit:
编辑:
more conceptual...
更多的概念……
map iterates down a list applying a function.
map迭代一个应用函数的列表。
map (+ 3) xs
uses the currying technique mentioned above. you could also:
使用上面提到的当前技术。你也可以:
map (\x -> x + 3) xs
to accomplish the same thing.
为了完成同样的事情。
#3
2
Simple example:
简单的例子:
rela :: Int -> [Int] -> [Int]rela x = map (\y -> (x-y)*(x-y))
Or might you want any perversions? -) Here you are with Applicatives:
或者你想要任何变态?-)这是您的申请表:
import Control.Applicativerela :: Int -> [Int] -> [Int]rela x = map $ (*) <$> (x-) <*> (x-)
#4
0
Hello I guess you mean this:
你好,我猜你的意思是:
Prelude> let rela n = map (\ x -> (x - n)^2)Prelude> rela 2 [3,5,7] [1,9,25]