Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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’


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Haskell: GHCi: Safely ignore warnings while running

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×