2017年2月1日水曜日

開発環境

関数プログラミング入門(Richard Bird (著)、山下伸夫 (翻訳)、オーム社)の第1章(基本概念)、1.1(セッションとスクリプト)、練習問題1.1.1、1.1.2、1.1.3を取り組んでみる。

練習問題1.1.1、1.1.2、1.1.3

コード(Emacs)

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

quad :: Float -> Float
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

入出力結果(Terminal, ghci)

$ ghci sample1.hs
GHCi, version 8.0.2: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Main             ( sample1.hs, interpreted )
Ok, modules loaded: Main.
*Main> quad 0
0.0
*Main> quad 1
1.0
*Main> quad 10
10000.0
*Main> quad -1

<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
*Main> quad (-1)
1.0
*Main> quad -10

<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
*Main> 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.
$ 

0 コメント:

コメントを投稿

Comments on Google+: