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

abd*_*tif -2 haskell functional-programming list similarity percentage

好的,所以我是 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]
Run Code Online (Sandbox Code Playgroud)

所以让我解释一下我想要做什么,我定义了我的函数来获取两个整数列表并输出一个浮点数(相似性百分比)。然后我编写我的函数,基本情况是当第一个列表为空时,而该函数会将第一个列表的每个元素与第二个列表的每个元素进行比较,如果找到匹配项,它将加 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’
Run Code Online (Sandbox Code Playgroud)

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

Jon*_*rdy 5

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
  …
Run Code Online (Sandbox Code Playgroud)

length返回一个Int.

例如,在 GHCi 中:

> :type length
length :: Foldable t => t a -> Int

> :set -XTypeApplications
> :type length @[]
length @[] :: [a] -> Int
Run Code Online (Sandbox Code Playgroud)

(请注意,我写是> 为了指示提示。您可以使用: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’
Run Code Online (Sandbox Code Playgroud)

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) => ()
Run Code Online (Sandbox Code Playgroud)

因此,您需要将结果length从 anInt转换为Floatwith fromIntegral。此函数可以返回 中的任何类型Num,并在:info Fractional上面的输出中注意这class Num a => Fractional a意味着Fractional是 的子集Num

> :type fromIntegral
fromIntegral :: (Integral a, Num b) => a -> b

> fromIntegral (length "abc") :: Float
3.0
Run Code Online (Sandbox Code Playgroud)

换句话说,您可以改写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’
  …
Run Code Online (Sandbox Code Playgroud)

当你写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’
  …
Run Code Online (Sandbox Code Playgroud)

tail是一个 type 的函数[a] -> [a],但是您将它作为 的第一个参数传递fun2,其第一个参数是一个 type 的值[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
  …
Run Code Online (Sandbox Code Playgroud)

最后,main必须是一个IO动作,按照惯例IO ()。它可能返回任何类型的结果,也就是说,您可以使用IO tfor any type t,结果值将被简单地丢弃。但是,您当前正在传递 a 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’
Run Code Online (Sandbox Code Playgroud)

解决方案是使用IO诸如的动作print (fun2 [1,2,3,4] [3,4,5,6]),它等价于putStrLn (show (fun2 [1,2,3,4] [3,4,5,6])),两者都将使用 debug-dump 类将 a 转换Float为 a ,并返回一个动作,该动作将在执行时将结果打印到标准输出。StringShowIOmain

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