Partial use of data constructor

I don’t understand why the following exercise "works" in Haskell programming from first principles:

type Subject = String
type Verb = String
type Object = String

data Sentence =
    Sentence Subject Verb Object
    deriving (Eq, Show)

s1 = Sentence "dogs" "drool"
s2 = Sentence "Julie" "loves" "dogs"

Downloading this in ghci shows that these typechecks are just fine, but why does the definition s1even make sense? I am still very new to Haskell, so at first I thought it was because in s1Haskell I implicitly skipped a line Objectblank. But then...

*Main> s1

<interactive>:13:1:
    No instance for (Show (Object -> Sentence))
      arising from a use of `print'
    Possible fix:
      add an instance declaration for (Show (Object -> Sentence))
    In a stmt of an interactive GHCi command: print it

I'm still learning how to correctly interpret these error messages, so please bear with me. But can anyone explain what it means No instance for (Show (Object -> Sentence))? More specifically, how does the (Object -> Sentence)string Objectin s1?

I'm sure this is stupid, but I don’t think this book made it clear to me at this point ...

+4
2

s1 ?

@Alec, currying. , , - GHCI, s1:

ghci> :t s1
s1 :: Object -> Sentence

, s1 - , Object a Sentence. - :

s1 = Sentence "dogs" "drool"

, x:

s1 x = Sentence "dogs" "drool" x

, s1 x, , Sentence , "dogs" "drool", x Sentence.

- , " (Show (Object -> Sentence))"?

- GHCI, , Haskell print .

ghci> 3+4

:

ghci> print (3+4)

( IO-, getLine print. Haskell IO.)

print - Show . , , s1 Object -> Sentence, Show .

, Show Sentence, GHC deriving (Eq, Show). , GHCI:

ghci> Sentence "Julie" "loves" "dogs"

:

Sentence "Julie" "loves" "dogs"

GHCI print (Sentence "Julie" "loves" "dogs").

, print ():

print x = putStrLn (show x)

show - , Show, .

+8
No instance for (Show (Object -> Sentence))
  arising from a use of `print'
Possible fix:
  add an instance declaration for (Show (Object -> Sentence))
In a stmt of an interactive GHCi command: print it

@ErikR: , GHC , , Show ( , , , ), . -, Haskell - , , Java- .

, Show ? Haskell wiki :

1. GHC , .. :

addOne num = num + 1
f x = x + 1
f y = y + 1  

, , ,

f x = x - x + x
f x = x

2. , .. (, ) .

f x = x + x

(1,2), (2,4) .. , GHC, ,

f x = x + x
g y = 2 * y

, show f show g , x y.

, ( GHC, Haskell), , :

{-# LANGUAGE ScopedTypeVariables #-}

import Data.Typeable

instance (Typeable a, Typeable b) => Show (a->b) where
  show _ = show $ typeOf (undefined :: a -> b)

> s1
[Char] -> Sentence

Object String ( type, String [Char]). , Object MkObject:

newtype Object = MkObject String deriving (Eq, Show)

s1 = Sentence "dogs" "drool"
s2 = Sentence "Julie" "loves" (MkObject "dogs")

voilà

> s1
Object -> Sentence
0

Source: https://habr.com/ru/post/1653073/


All Articles