Haskell程序来查找两个列表之间的相似性



好吧,所以我是Haskell的新手,我想写一个程序,我拿两个列表并找到它们之间的相似性(公共项目数/项目数)。 这是我到目前为止所拥有的:

fun2 :: [Int]->[Int]->Float
fun2 [] xs2 = 0
fun2 xs1 xs2 = if head xs1 == head xs2 then (1/length xs2) + fun2 tail xs1 xs2
else if head xs1 /= head xs2 then fun2 xs1 tail xs2
else fun2 tail xs1 xs2
main = fun2 [1,2,3,4] [3,4,5,6]

所以让我解释一下我要做什么,我定义了我的函数来获取两个整数列表并输出一个浮点数(相似性百分比)。 然后我写我的函数,基本情况是当第一个列表为空时,而函数会将第一个列表的每个元素与第二个列表的每个元素进行比较, 如果它找到匹配项,它将加 1 然后除以大小以获得百分比。 但是,当我运行此代码时,我收到很多错误:

main.hs:4:45: error:
• Couldn't match expected type ‘Float’ with actual type ‘Int’
• In the expression: (1 / length xs2) + fun2 tail xs1 xs2
In the expression:
if head xs1 == head xs2 then
(1 / length xs2) + fun2 tail xs1 xs2
else
if head xs1 /= head xs2 then
fun2 xs1 tail xs2
else
fun2 tail xs1 xs2
In an equation for ‘fun2’:
fun2 xs1 xs2
= if head xs1 == head xs2 then
(1 / length xs2) + fun2 tail xs1 xs2
else
if head xs1 /= head xs2 then
fun2 xs1 tail xs2
else
fun2 tail xs1 xs2
main.hs:4:62: error:
• Couldn't match expected type ‘[Int] -> Int’
with actual type ‘Float’
• The function ‘fun2’ is applied to three arguments,
but its type ‘[Int] -> [Int] -> Float’ has only two
In the second argument of ‘(+)’, namely ‘fun2 tail xs1 xs2’
In the expression: (1 / length xs2) + fun2 tail xs1 xs2
main.hs:4:67: error:
• Couldn't match expected type ‘[Int]’
with actual type ‘[a0] -> [a0]’
• Probable cause: ‘tail’ is applied to too few arguments
In the first argument of ‘fun2’, namely ‘tail’
In the second argument of ‘(+)’, namely ‘fun2 tail xs1 xs2’
In the expression: (1 / length xs2) + fun2 tail xs1 xs2
main.hs:5:39: error:
• Couldn't match expected type ‘[Int] -> Float’
with actual type ‘Float’
• The function ‘fun2’ is applied to three arguments,
but its type ‘[Int] -> [Int] -> Float’ has only two
In the expression: fun2 xs1 tail xs2
In the expression:
if head xs1 /= head xs2 then
fun2 xs1 tail xs2
else
fun2 tail xs1 xs2
main.hs:5:48: error:
• Couldn't match expected type ‘[Int]’
with actual type ‘[a1] -> [a1]’
• Probable cause: ‘tail’ is applied to too few arguments
In the second argument of ‘fun2’, namely ‘tail’
In the expression: fun2 xs1 tail xs2
In the expression:
if head xs1 /= head xs2 then
fun2 xs1 tail xs2
else
fun2 tail xs1 xs2
main.hs:6:10: error:
• Couldn't match expected type ‘[Int] -> Float’
with actual type ‘Float’
• The function ‘fun2’ is applied to three arguments,
but its type ‘[Int] -> [Int] -> Float’ has only two
In the expression: fun2 tail xs1 xs2
In the expression:
if head xs1 /= head xs2 then
fun2 xs1 tail xs2
else
fun2 tail xs1 xs2
main.hs:6:15: error:
• Couldn't match expected type ‘[Int]’
with actual type ‘[a2] -> [a2]’
• Probable cause: ‘tail’ is applied to too few arguments
In the first argument of ‘fun2’, namely ‘tail’
In the expression: fun2 tail xs1 xs2
In the expression:
if head xs1 /= head xs2 then
fun2 xs1 tail xs2
else
fun2 tail xs1 xs2
main.hs:8:1: error:
• Couldn't match expected type ‘IO t0’ with actual type ‘Float’
• In the expression: main
When checking the type of the IO action ‘main’

所以你能告诉我我在这里做错了什么吗?

main.hs:4:45: error:
• Couldn't match expected type ‘Float’ with actual type ‘Int’
• In the expression: (1 / length xs2) + fun2 tail xs1 xs2
…

length返回一个Int

例如,在 GHCi 中:

> :type length
length :: Foldable t => t a -> Int
> :set -XTypeApplications
> :type length @[]
length @[] :: [a] -> Int

(请注意,我写>来指示提示。您可以使用:set prompt "> "将提示设置为相同,对于多行输入,:set prompt-cont "| "

虽然(/)适用于集合中的类型Fractional

> :type (/)
(/) :: Fractional a => a -> a -> a
> :info Fractional
class Num a => Fractional a where
(/) :: a -> a -> a
recip :: a -> a
fromRational :: Rational -> a
{-# MINIMAL fromRational, (recip | (/)) #-}
-- Defined in ‘GHC.Real’
instance forall a k (b :: k).
Fractional a =>
Fractional (Const a b)
-- Defined in ‘Data.Functor.Const’
instance Fractional Float -- Defined in ‘GHC.Float’
instance Fractional Double -- Defined in ‘GHC.Float’

Int不在Fractional

> :set -XFlexibleContexts
> () :: (Fractional Int) => ()
<interactive>:…:1: error:
• No instance for (Fractional Int)
arising from an expression type signature
• In the expression: () :: (Fractional Int) => ()
In an equation for ‘it’: it = () :: (Fractional Int) => ()

因此,您需要将length的结果从Int转换为具有fromIntegralFloat。此函数可以返回Num中的任何类型,并在上面的:info Fractional的输出中注意class Num a => Fractional a表示FractionalNum的子集。

> :type fromIntegral
fromIntegral :: (Integral a, Num b) => a -> b
> fromIntegral (length "abc") :: Float
3.0

换句话说,您可以改为编写1 / fromIntegral (length xs2)。注意括号!这将我们引出接下来的几个错误消息:

main.hs:4:62: error:
• Couldn't match expected type ‘[Int] -> Int’
with actual type ‘Float’
• The function ‘fun2’ is applied to three arguments,
but its type ‘[Int] -> [Int] -> Float’ has only two
In the second argument of ‘(+)’, namely ‘fun2 tail xs1 xs2’
…

当你写fun2 tail xs1 xs2时,这意味着"fun2应用于三个参数:tailxs1xs2"。您想将tail xs1的结果作为单个参数传递,因此您需要括号将它们组合在一起,即fun2 (tail xs1) xs2.这也是下一个错误的原因:

main.hs:4:67: error:
• Couldn't match expected type ‘[Int]’
with actual type ‘[a0] -> [a0]’
• Probable cause: ‘tail’ is applied to too few arguments
In the first argument of ‘fun2’, namely ‘tail’
In the second argument of ‘(+)’, namely ‘fun2 tail xs1 xs2’
…

tail是类型[a] -> [a]的函数,但你将其作为fun2的第一个参数传递,其第一个参数是类型[Int]的值。由于fun2的其他调用中的相同错误,相同的错误消息重复出现。

例如,下一个表达式应读取fun2 xs1 (tail xs2)。下一个错误也有相同的根本原因,并且还为您提供了有关如何解决它的良好提示:

main.hs:6:10: error:
• Couldn't match expected type ‘[Int] -> Float’
with actual type ‘Float’
•The function ‘fun2’ is applied to three arguments,
but its type ‘[Int] -> [Int] -> Float’ has only two
In the expression: fun2 tail xs1 xs2
…

最后,main必须是IO行动,传统上IO ()。它可以返回任何类型的结果,也就是说,您可以将IO t用于任何类型的t,并且结果值将被简单地丢弃。但是,您当前正在传递一个Float,这是调用fun2的结果,因此不匹配:

main.hs:8:1: error:
• Couldn't match expected type ‘IO t0’ with actual type ‘Float’
• In the expression: main
When checking the type of the IO action ‘main’

解决方案是使用一个IO动作,如print (fun2 [1,2,3,4] [3,4,5,6]),它相当于putStrLn (show (fun2 [1,2,3,4] [3,4,5,6])),两者都将使用调试转储类ShowFloat转换为String,并返回一个IO动作,该动作将在执行main时将结果打印到标准输出。

GHC的错误消息并不总是完美的,但幸运的是,所有这些错误消息都包含足够的信息来解决您的问题。你只需要更多的练习阅读它们,理解他们在说什么以及如何继续。

最新更新