7

xmonadを構成していますが、数十のインスタンスを開始する必要があるため、xとyの位置、幅、高さ、テキストの整列のパラメーターを受け取る関数を使用する方がよいと判断しました。

-- mydzen.hs

import Data.List

-- | Return a string that launches dzen with the given configuration.
myDzen :: Num a  => a -> a -> a -> Char -> String -> String

myDzen y x w ta e =
        intercalate " "
            [ "dzen2"
            , "-x"  , show x
            , "-w"  , show w
            , "-y"  , show y
            , "-h"  , myHeight
            , "-fn" , quote myFont
            , "-bg" , quote myDBGColor
            , "-fg" , quote myFFGColor
            , "-ta" , [ta]
            , "-e"  , quote e
            ]

quote :: String -> String
quote x = "'" x "'"

-- dummy values
myHeigth = "20"
myFont = "bitstream"
myDBGColor = "#ffffff"
myFFGColor = "#000000"

Could not deduce (Show a) arising from a use of `show'
from the context (Num a)
  bound by the type signature for
             myDzen :: Num a => a -> a -> a -> Char -> String -> String
  at mydzen.hs:(5,1)-(17,13)
Possible fix:
  add (Show a) to the context of
    the type signature for
      myDzen :: Num a => a -> a -> a -> Char -> String -> String
In the expression: show x
In the second argument of `intercalate', namely
  `["dzen2", "-x", show x, "-w", ....]'
In the expression:
  intercalate " " ["dzen2", "-x", show x, "-w", ....]

もちろん、署名を削除したり、に変更Num aしたりShow aすると問題は解決しますが、その理由はわかりません。'引数' xwおよびほとんどすべての種類の数(、、 ecc)であると想定されていyます100550.21366 * 0.7

私はhaskellを初めて使用しますが、これまでエラーを(明確に)理解したり、何が問題なのかを見つけることができませんでした。

4

3 に答える 3

18

以前は、ShowEqスーパークラスでNumあり、コードはコンパイルされていました。

新しいGHCでは、バージョン7.4からこれが変更され、とにNum依存しなくなったため、署名に追加する必要があります。ShowEq

その理由は、関心の分離です。意味のある平等性がなく、関数を表示する数値型(計算可能な実数、関数のリング)があります。

于 2012-06-25T18:15:00.587 に答える
7

すべての数値が表示できるわけではありません(Intまたはのような標準の型Integerですが、独自の型を定義できます。コンパイラはどのように認識しますか?)show x関数で使用しているため、typeclassaに属している必要があります。Show署名をに変更してmyDzen :: (Num a, Show a) => a -> a -> a -> Char -> String -> String、エラーを取り除くことができます。

于 2012-06-25T16:28:00.537 に答える
3

showは型クラスの一部ではありませんNum—それを使用できるようにするには、次Showのものと一緒に制約を追加する必要がありますNum

myDzen :: (Num a, Show a) => a -> a -> a -> Char -> String -> String
于 2012-06-25T16:28:07.270 に答える