Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

the list is:

l = ["1","kg","g"]

I found this method but it didn't work:

    map (convert) l
    convert [x, "kg", "g"] = (read x :: Integer) *1000

I got the following error:

    parse error on input `='
    Perhaps you need a 'let' in a 'do' block?
    e.g. 'let x = 5' instead of 'x = 5'
   |
17 |     convert [x, "kg", "g"] = (read x :: Integer) *1000
   |                            ^                ^

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
3.6k views
Welcome To Ask or Share your Answers For Others

1 Answer

You can not convert values with a typehint. Haskell has a strong type system, which means that you can only use functions to turn a String in an Integer for example.

Since Integer is an instance of the Read typeclass, you can use read :: Read a => String -> a to parse a string to an Integer:

convert [x, "kg", "g"] = (read x :: Integer) *1000

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share

548k questions

547k answers

4 comments

56.5k users

...