Newbie: Type Conversion in Haskell

The first message is here, please go to me. I found several threads with similar problems, none of them were applied directly, or if this were done, the execution was far enough above my head.

If I have a code p=['1','2','3','4']that stores numbers as characters in p, how do I create a list qthat can equal [1,2,3,4]? I tried all kinds of things, mainly when I was absent outside the scope or any function, I try to convert Char -> Int, devoid of an accompanying binding. It seems that everywhere I find an indication that there is such a thing as digitToIntwhere I digitToInt '1'should give an output 1, but I apparently have no bindings, even with the exact input from this page: http://zvon.org/other/haskell /Outputchar/digitToInt_f.html

At this moment, reading more things, I just become more confused. Please help with a viable solution that can show me where I got confused or explained why this digitToInt :: Char -> Intone doesn't seem to work for me in the least.

Thank.

+4
source share
2 answers

digitToInt- this is what already exists. He lived in a module Char, but now he lives in Data.Char, so we must import Data.Charuse it.

Prelude> import Data.Char
Prelude Data.Char> digitToInt '1'
1

You can use digitToIntfor each item in the list with map digitToInt. map :: (a->b) -> [a] -> [b]applies a function (a->b)to each element of the list as, [a], to get a list of bs, [b].

Prelude Data.Char> map digitToInt ['1', '2', '3', '4']
[1,2,3,4]

Missing accompanying binding

digitToInt , digitToInt :: Char -> Int. ,

alwaysSeven :: Char -> Int

.

The type signature for `alwaysSeven' lacks an accompanying binding

, .

alwaysSeven :: Char -> Int
alwaysSeven x = 7
+9

, ((:[])) ( ) read :

map (read . (:[])) "1234"

, , . :

(map (read . (:[])) "1234") :: [Int]
-- [1,2,3,4]
(map (read . (:[])) "1234") :: [Double]
-- [1.0,2.0,3.0,4.0]
0

Source: https://habr.com/ru/post/1569477/


All Articles