1

私は関数型プログラミングが初めてで、Haskell でスタックを作成して表示しようとしています。プログラムで構築しているスタックを表示させたいのですが。これは私のコードです:

module Stack (Stack, empty, push, pop, top, isEmpty) where
    data Stack a       = EmptyStack | Stk a (Stack a)
    push x s           = Stk x s
    top (Stk x s)      = x
    pop (Stk _ s)      = s
    empty              = EmptyStack
    isEmpty EmptyStack = True
    isEmpty (Stk x s)  = False`

    instance Show a => Show (Stack a) where
    show EmptyStack = "|"
    show (Stk a b) = (show a) ++ " <- " ++ (show b)

「show (push 1 empty)」を使用すると、「 1 <- | 」のような答えが (多かれ少なかれ) 期待できますが、コードをコンパイルできません。試してみると、次のエラーが表示されます。

[1 of 1] Compiling Stack            ( Stack.hs, interpreted )

Stack.hs:12:27:
    Ambiguous occurrence ‘show’
    It could refer to either ‘Stack.show’, defined at Stack.hs:11:9
                      or ‘Prelude.show’,
                         imported from ‘Prelude’ at Stack.hs:1:8-12
                         (and originally defined in ‘GHC.Show’)

Stack.hs:12:47:
    Ambiguous occurrence ‘show’
    It could refer to either ‘Stack.show’, defined at Stack.hs:11:9
                      or ‘Prelude.show’,
                         imported from ‘Prelude’ at Stack.hs:1:8-12
                         (and originally defined in ‘GHC.Show’)
Failed, modules loaded: none.

プログラムが Prelude の「show」と be で定義された「show」を混同する可能性があるエラーは理解していますが、コードでそのエラーを確認できません。その上、いくつかの仲間は同じコードを持っており、プログラムはうまく動作します.

変更しなければならないもの、または見逃したものはありますか?

ありがとう!

4

1 に答える 1