Friday 29 April 2016

Haskell: GHCi: Safely ignore warnings while running

Consider following program, lengthOfList function takes list of any type and return number of elements in the list. Don’t worry much about the program, I will explain about this in my later posts.


ListUtil.hs
lengthOfList :: [a] -> Integer
lengthOfList [] = 0
lengthOfList (x:xs) = 1 + lengthOfList xs

Prelude> :load ListUtil.hs
[1 of 1] Compiling Main             ( ListUtil.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> lengthOfList "abcd"
4


Try to run lengthOfList function on list of numbers, If warnings are enabled on your machine, you will get following kind of output.
*Main> lengthOfList [1, 2, 3]

<interactive>:9:15: Warning:
    Defaulting the following constraint(s) to type Integer
      (Num a0) arising from the literal 1
    In the expression: 1
    In the first argument of lengthOfList, namely [1, 2, 3]
    In the expression: lengthOfList [1, 2, 3]

<interactive>:9:15: Warning:
    Defaulting the following constraint(s) to type Integer
      (Num a0) arising from the literal 1
    In the expression: 1
    In the first argument of lengthOfList, namely [1, 2, 3]
    In the expression: lengthOfList [1, 2, 3]
3


Warning is telling you that Integer is the default choice, Same will happen to real numbers also.
*Main> lengthOfList [1.1, 2.2, 3.3]

<interactive>:11:15: Warning:
    Defaulting the following constraint(s) to type Double
      (Fractional a0) arising from the literal 1.1
    In the expression: 1.1
    In the first argument of lengthOfList, namely [1.1, 2.2, 3.3]
    In the expression: lengthOfList [1.1, 2.2, 3.3]

<interactive>:11:15: Warning:
    Defaulting the following constraint(s) to type Double
      (Fractional a0) arising from the literal 1.1
    In the expression: 1.1
    In the first argument of lengthOfList, namely [1.1, 2.2, 3.3]
    In the expression: lengthOfList [1.1, 2.2, 3.3]
3

Warning is telling you, Double is the default choice.

How to disable above warnings ?
There are two ways.

a. Use the command ‘:set –w’
*Main> :set -w
*Main> lengthOfList [1.1, 2.2, 3.3]
3
*Main> lengthOfList [1, 2, 3]
3

b. Open GHCi prompt using ‘ghci -fno-warn-type-defaults’
$ ghci -fno-warn-type-defaults
GHCi, version 7.10.3: http://www.haskell.org/ghc/  :? for help
Prelude> 
Prelude> :load ListUtil.hs
[1 of 1] Compiling Main             ( ListUtil.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> lengthOfList [1, 2, 3]
3
*Main> lengthOfList [1.1, 2.2, 3.3]
3

If you want to see the warnings use ‘:set –Wall’.
*Main> :set -Wall
*Main> 
*Main> lengthOfList [1.1, 2.2, 3.3]

<interactive>:10:15: Warning:
    Defaulting the following constraint(s) to type Double
      (Fractional a0) arising from the literal 1.1
    In the expression: 1.1
    In the first argument of lengthOfList, namely [1.1, 2.2, 3.3]
    In the expression: lengthOfList [1.1, 2.2, 3.3]

<interactive>:10:15: Warning:
    Defaulting the following constraint(s) to type Double
      (Fractional a0) arising from the literal 1.1
    In the expression: 1.1
    In the first argument of lengthOfList, namely [1.1, 2.2, 3.3]
    In the expression: lengthOfList [1.1, 2.2, 3.3]
3








Previous                                                 Next                                                 Home

No comments:

Post a Comment