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 template:
createGroceryItem (a:b:c) = ...
Here : is a (right-associative) binary operator that adds an item to the list. RHS item 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 considered as a list of strings. Obviously, it cannot be read or passed to any functions that expect String.
You should use instead
createGroceryItem [a, b, c] = ...
if the list should have exactly 3 elements or
createGroceryItem (a:b:c:xs) = ...
if valid ≥3.
Also (2) expression
makeGroceryItem a read b read c
will be interpreted as makeGroceryItem using 5 arguments, 2 of which are read functions. You need to use brackets:
makeGroceryItem a (read b) (read c)
kennytm Mar 18 2018-10-18T00: 00Z
source share