juan_gandhi: (Default)
Juan-Carlos Gandhi ([personal profile] juan_gandhi) wrote2012-07-02 12:24 pm
Entry tags:

наука и жизнь

Scala:
  scala> List(Set(1,2)).flatten == List(Set(2,1)).flatten
res3: Boolean = false


Бля.

[identity profile] eacher.livejournal.com 2012-07-03 02:20 pm (UTC)(link)
a prosvetite ubogogo, please...

chem Data.HashSet luchshe chem Data.Set?

v kakih takih cluchajah pol'zujut? nabor funcij vrode odin i tot zhe

===========================

P.S.

ksta, otkompilit' vash primer ne daet:



--  sets1.hs

import qualified Data.Set as S
import qualified Data.HashSet as H

z =
  S.toList (S.fromList [4, 4 + 2 ^ 39]) ==
  S.toList (S.fromList [4 + 2 ^ 39, 4])

z' =
  H.toList (H.fromList [4, 4 + 2 ^ 39]) ==
  H.toList (H.fromList [4 + 2 ^ 39, 4])



i vot, v ghci:

*Main> :r
[1 of 1] Compiling Main ( sets1.hs, interpreted )

sets1.hs:54:15:
Ambiguous type variable `a0' in the constraints:
(Data.Hashable.Hashable a0) arising from a use of `H.fromList'
at sets1.hs:54:15-24
(Num a0) arising from the literal `4' at sets1.hs:54:39
(Eq a0) arising from a use of `H.fromList' at sets1.hs:54:15-24
Probable fix: add a type signature that fixes these type variable(s)
In the second argument of `($)', namely
`H.fromList [4 + 2 ^ 39, 4]'
In the second argument of `(==)', namely
`(H.toList $ H.fromList [4 + 2 ^ 39, 4])'
In the expression:
(H.toList $ H.fromList [4, 4 + 2 ^ 39])
==
(H.toList $ H.fromList [4 + 2 ^ 39, 4])
Failed, modules loaded: none.

===================================

P.P.S.

a vot esli tak, to compilitsa, no rezul'tat : true



z' =
  (H.toList $ H.fromList [4::Int, 4 + 2 ^ 39]) ==
  (H.toList $ H.fromList [4 + 2 ^ 39, 4::Int])

*Main> :r
Ok, modules loaded: Main.
*Main> z'
True
*Main>


Edited 2012-07-03 14:39 (UTC)

[identity profile] nponeccop.livejournal.com 2012-07-03 02:40 pm (UTC)(link)
См. http://www.haskell.org/ghc/docs/latest/html/users_guide/interactive-evaluation.html раздел 2.4.7. Type defaulting in GHCi.

В GHCi действуют другие правила для тайпчекера, поэтому если я набираю в промпте

toList (fromList [4, 4 + 2 ^ 39]) == toList (fromList [4 + 2 ^ 39, 4])

- оно работает, а при переносе в исходник требуется указать, какие именно целые числа имеются ввиду. Например, так

zz = S.toList (S.fromList [4::Integer, 4 + 2 ^ 39]) == S.toList (S.fromList [4 + 2 ^ 39, 4])

[identity profile] nponeccop.livejournal.com 2012-07-03 02:42 pm (UTC)(link)
> a vot esli tak, to compilitsa, no rezul'tat : true

Конечно будет true, т.к. 4::Int + 2 ^ 39 == 4 на архитектурах с 32-битными интами:

Prelude> [4 + 2 ^ 39, 4::Int]
[4,4]
Prelude> [4 + 2 ^ 39, 4::Integer]
[549755813892,4]

[identity profile] eacher.livejournal.com 2012-07-03 02:50 pm (UTC)(link)
da, s typecheck ja teper' ponjal, spasibo

a zachem ispol'zovat' Data.HashSet

v chem bonus?