Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell implicit conversions

Hello i was looking to using Data.Text.intercalate and from Hackage i do not understand why if the method has the following signature:

intercalate :: Text -> [Text] -> Text

Why then, does this work

T.intercalate "NI!" ["We", "seek", "the", "Holy", "Grail"]
"WeNI!seekNI!theNI!HolyNI!Grail" 

Shouldn't you apply Data.Text.pack it before each element of the list?
Source : http://hackage.haskell.org/package/text-1.2.3.1/docs/Data-Text.html

In my case i want to pack the following :

Input :"{" ,mytext ,"}" #mytext::Text
I am doing it with :
Prelude.intercalate (Data.Text.pack ",") [pack "{",mytext, pack "}"] or

(pack "{") ++ mytext++ pack "}")
Can someone please explain me why does Data.Text expose the same methods as Data.List (in our case intercalate) and how does it make implicit conversions between Char and Text ?

like image 427
Bercovici Adrian Avatar asked Dec 09 '25 17:12

Bercovici Adrian


1 Answers

You likely enabled -XOverloadedStrings (or enabled it with the {-# LANGUAGE OverloadedStrings #-} at the top of the file).

As a result this means that string literals (not string variables, only the literals), can be interpreted by any IsString type.

Text is an IsString type. So that means that implicitly you use pack around the string literals (again literals, not ordinary variables).

A similar thing happens with number literals: a number literal can be any Num type. Based on what functions you call on the number literal, Haskell can derive the exact type, and thus "interprets" the literal accordingly. For example if you write atan2 1 2, then 1 and 2 should be interpreted as RealFloat types, whereas for quot 1 2, the 1 and 2 are interpreted as Ìntegral` types.

like image 88
Willem Van Onsem Avatar answered Dec 12 '25 14:12

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!