string - Haskell: "read" function doesn't handle "+" for different types? -


this 1 ok, no problem

prelude> read "8.2" + 3.4 11.6 

but 1 not ok

prelude> read "8.2"+"3.4" <interactive>:69:11: no instance (num [char]) arising use of ‘+’ in expression: read "8.2" + "3.4" in equation ‘it’: = read "8.2" + "3.4" 

and neither one:

prelude> read "8.2" + 34  *** exception: prelude.read: no parse 

why? tried:

read "8.2"::float + 3.4 ::float  

doesn't work either.

what input requirement of "read"? first 1 string, other 1 none-string, in order math?

your assumption read works on rest of line wrong!

read :: read => string -> 

it takes string applied , not succeeding it, try

(read "3.5") + (read "3.5") :: float 

note parenthesis, i'd recommend taking @ learn haskell great good - solid introduction haskell.

  • read "8.2"+"3.4" not work because, try add string ("3.4") , compiler tells + expects left , right hand side equal looks plus-instance string (a.k.a. [char]) because right hand side of + is.

  • read "8.2" + 34 not work because haskell defaults integer unspecified num types (34 :: num => a), , tries parse "8.2" integer , fails because integers don't have dots in them.

  • the last 1 right, have add parens make work (see above)

your headline includes wrong assumption:

the arguments of + have have same type

(+) :: num => -> -> 

the type signature says: "numeric" (thats interface if know java) can use + function , first, second argument result of same type.

if want have conversion have make manually/explicitly


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -