2017年2月1日水曜日

コード(Emacs)

```-- 1.1.1
square :: Float -> Float
square x = x * x

quad x = square ( square x )

-- 1.1.2
greater :: Float -> Float -> Float
greater x y = if x >= y then x else y

-- 1.1.3
area :: Float -> Float
area r = r * r * 22/7
```

```\$ ghci sample1.hs
GHCi, version 8.0.2: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Main             ( sample1.hs, interpreted )
0.0
1.0
10000.0

<interactive>:4:1: error:
‚Ä¢ No instance for (Num (Float -> Float)) arising from a use of ‚Äò-‚Äô
(maybe you haven't applied a function to enough arguments?)
‚Ä¢ In the expression: quad - 1
In an equation for ‚Äòit‚Äô: it = quad - 1
1.0

<interactive>:6:1: error:
‚Ä¢ No instance for (Num (Float -> Float)) arising from a use of ‚Äò-‚Äô
(maybe you haven't applied a function to enough arguments?)
‚Ä¢ In the expression: quad - 10
In an equation for ‚Äòit‚Äô: it = quad - 10
10000.0
*Main> greater 0 1
1.0
*Main> greater 1 0
1.0
*Main> greater 1 1
1.0
*Main> greater -1 0

<interactive>:11:1: error:
‚Ä¢ No instance for (Num (Float -> Float -> Float))
arising from a use of ‚Äò-‚Äô
(maybe you haven't applied a function to enough arguments?)
‚Ä¢ In the expression: greater - 1 0
In an equation for ‚Äòit‚Äô: it = greater - 1 0

<interactive>:11:10: error:
‚Ä¢ No instance for (Num (t0 -> Float -> Float -> Float))
arising from the literal ‚Äò1‚Äô
(maybe you haven't applied a function to enough arguments?)
‚Ä¢ In the expression: 1
In the second argument of ‚Äò(-)‚Äô, namely ‚Äò1 0‚Äô
In the expression: greater - 1 0

<interactive>:11:12: error:
‚Ä¢ Ambiguous type variable ‚Äòt0‚Äô arising from the literal ‚Äò0‚Äô
prevents the constraint ‚Äò(Num t0)‚Äô from being solved.
Probable fix: use a type annotation to specify what ‚Äòt0‚Äô should be.
These potential instances exist:
instance Num Integer -- Defined in ‚ÄòGHC.Num‚Äô
instance Num Double -- Defined in ‚ÄòGHC.Float‚Äô
instance Num Float -- Defined in ‚ÄòGHC.Float‚Äô
...plus two others
...plus one instance involving out-of-scope types
(use -fprint-potential-instances to see them all)
‚Ä¢ In the first argument of ‚Äò1‚Äô, namely ‚Äò0‚Äô
In the second argument of ‚Äò(-)‚Äô, namely ‚Äò1 0‚Äô
In the expression: greater - 1 0
*Main> greater (-1) 0
0.0
*Main> greater -1.0 0

<interactive>:13:1: error:
‚Ä¢ No instance for (Num (Float -> Float -> Float))
arising from a use of ‚Äò-‚Äô
(maybe you haven't applied a function to enough arguments?)
‚Ä¢ In the expression: greater - 1.0 0
In an equation for ‚Äòit‚Äô: it = greater - 1.0 0

<interactive>:13:10: error:
‚Ä¢ No instance for (Fractional (t0 -> Float -> Float -> Float))
arising from the literal ‚Äò1.0‚Äô
(maybe you haven't applied a function to enough arguments?)
‚Ä¢ In the expression: 1.0
In the second argument of ‚Äò(-)‚Äô, namely ‚Äò1.0 0‚Äô
In the expression: greater - 1.0 0

<interactive>:13:14: error:
‚Ä¢ Ambiguous type variable ‚Äòt0‚Äô arising from the literal ‚Äò0‚Äô
prevents the constraint ‚Äò(Num t0)‚Äô from being solved.
Probable fix: use a type annotation to specify what ‚Äòt0‚Äô should be.
These potential instances exist:
instance Num Integer -- Defined in ‚ÄòGHC.Num‚Äô
instance Num Double -- Defined in ‚ÄòGHC.Float‚Äô
instance Num Float -- Defined in ‚ÄòGHC.Float‚Äô
...plus two others
...plus one instance involving out-of-scope types
(use -fprint-potential-instances to see them all)
‚Ä¢ In the first argument of ‚Äò1.0‚Äô, namely ‚Äò0‚Äô
In the second argument of ‚Äò(-)‚Äô, namely ‚Äò1.0 0‚Äô
In the expression: greater - 1.0 0
*Main> greater (-1) (-1)
-1.0
*Main> area 0
0.0
*Main> area 1
3.142857
*Main> area 10
314.2857
*Main> :q
Leaving GHCi.
\$
```