PHP 的关联数组的 Haskell 等价是什么
What is the Haskell equivalence of PHP's associative array
在PHP我可以做到:
$array = [
'A' => [1,2,3],
'B' => [4,5,6],
'C' => [7,8,9],
]
PHP 的关联数组的 Haskell 等价是什么?
我的印象是您正在寻找值映射数据结构的键。在那种情况下 Haskell 有 Data.Map
provided by the containers
package. There are a few variations to it including Data.Map.Strict, Data.Map.Lazy, and Data.IntMap.
Haskell 的默认地图实现是有序的,并基于平衡树,这使得操作具有 对数 时间复杂度。但是 unordered-containers
包中还有一个散列实现,它提供了 恒定的 操作时间,但你当然不会得到默认的键顺序。
您提供的 PHP 关联数组示例之后的一个简短示例:
import qualified Data.Map.Strict as Map
myMap = Map.fromList [ ('A',[1,2,3])
, ('B',[4,5,6])
, ('C',[7,8,9])
]
-- Looking up an existing key
Map.lookup 'B' myMap
> Just [4,5,6]
-- Looking up a non-existing key
Map.lookup 'Z' myMap
> Nothing
一些关于如何在 Haskell 中使用 Map
的更多上下文取自 Data.Map
的文档:
import qualified Data.Map.Strict as Map
nums = Map.fromList [(1,"one"), (2,"two"), (3,"three")]
-- Get the English word for the number 3 and 4.
Map.lookup 3 nums
> Just "three"
Map.lookup 4 nums
> Nothing
-- Add (4, "four") to our original map.
moreNums = Map.insert 4 "four" nums
Map.member 4 moreNums
> True
-- Remove the entry for 1 from our original map.
fewerNums = Map.delete 1 nums
Map.toAscList fewerNums
> [(2,"two"),(3,"three")]
-- Create a new map and combine it with our original map.
-- fromList is right-biased: if a key is repeated the rightmost value is taken.
newNums = Map.fromList [(3,"new three"), (4,"new four"), (4,"newer four")]
-- union is left-biased: if a key occurs more than once the value from the
-- left map is taken.
Map.union newNums nums
> fromList [(1,"one"),(2,"two"),(3,"new three"),(4,"newer four")]
通常 Data.Map
最适合这类东西,但我要指出的是,简单地使用 元组列表 也很有意义,具体取决于希望你打算这样做。
array = [ ('A', [1,2,3])
, ('B', [4,5,6])
, ('C', [7,8,9]) ]
将此称为 array 更明智,尽管 IMO 术语“关联”和“数组”相互矛盾。无论如何,您可以在此类列表上使用标准 lookup
函数,请注意它具有 O (n) 复杂度O (1) 或 O (log n) 是专门设计的结构实现的。
在PHP我可以做到:
$array = [
'A' => [1,2,3],
'B' => [4,5,6],
'C' => [7,8,9],
]
PHP 的关联数组的 Haskell 等价是什么?
我的印象是您正在寻找值映射数据结构的键。在那种情况下 Haskell 有 Data.Map
provided by the containers
package. There are a few variations to it including Data.Map.Strict, Data.Map.Lazy, and Data.IntMap.
Haskell 的默认地图实现是有序的,并基于平衡树,这使得操作具有 对数 时间复杂度。但是 unordered-containers
包中还有一个散列实现,它提供了 恒定的 操作时间,但你当然不会得到默认的键顺序。
您提供的 PHP 关联数组示例之后的一个简短示例:
import qualified Data.Map.Strict as Map
myMap = Map.fromList [ ('A',[1,2,3])
, ('B',[4,5,6])
, ('C',[7,8,9])
]
-- Looking up an existing key
Map.lookup 'B' myMap
> Just [4,5,6]
-- Looking up a non-existing key
Map.lookup 'Z' myMap
> Nothing
一些关于如何在 Haskell 中使用 Map
的更多上下文取自 Data.Map
的文档:
import qualified Data.Map.Strict as Map
nums = Map.fromList [(1,"one"), (2,"two"), (3,"three")]
-- Get the English word for the number 3 and 4.
Map.lookup 3 nums
> Just "three"
Map.lookup 4 nums
> Nothing
-- Add (4, "four") to our original map.
moreNums = Map.insert 4 "four" nums
Map.member 4 moreNums
> True
-- Remove the entry for 1 from our original map.
fewerNums = Map.delete 1 nums
Map.toAscList fewerNums
> [(2,"two"),(3,"three")]
-- Create a new map and combine it with our original map.
-- fromList is right-biased: if a key is repeated the rightmost value is taken.
newNums = Map.fromList [(3,"new three"), (4,"new four"), (4,"newer four")]
-- union is left-biased: if a key occurs more than once the value from the
-- left map is taken.
Map.union newNums nums
> fromList [(1,"one"),(2,"two"),(3,"new three"),(4,"newer four")]
通常 Data.Map
最适合这类东西,但我要指出的是,简单地使用 元组列表 也很有意义,具体取决于希望你打算这样做。
array = [ ('A', [1,2,3])
, ('B', [4,5,6])
, ('C', [7,8,9]) ]
将此称为 array 更明智,尽管 IMO 术语“关联”和“数组”相互矛盾。无论如何,您可以在此类列表上使用标准 lookup
函数,请注意它具有 O (n) 复杂度O (1) 或 O (log n) 是专门设计的结构实现的。