Convert String to Integer/Float in Haskell?

read can parse a string into float and int:

Prelude> :set +t
Prelude> read "123.456" :: Float
123.456
it :: Float
Prelude> read "123456" :: Int
123456
it :: Int

But the problem (1) is in your pattern:

createGroceryItem (a:b:c) = ...

Here : is a (right-associative) binary operator which prepends an element to a list. The RHS of an element must be a list. Therefore, given the expression a:b:c, Haskell will infer the following types:

a :: String
b :: String
c :: [String]

i.e. c will be thought as a list of strings. Obviously it can’t be read or passed into any functions expecting a String.

Instead you should use

createGroceryItem [a, b, c] = ...

if the list must have exactly 3 items, or

createGroceryItem (a:b:c:xs) = ...

if ≥3 items is acceptable.

Also (2), the expression

makeGroceryItem a read b read c

will be interpreted as makeGroceryItem taking 5 arguments, 2 of which are the read function. You need to use parenthesis:

makeGroceryItem a (read b) (read c)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)