gpt4 book ai didi

dictionary - 为什么这个字典功能不起作用

转载 作者:行者123 更新时间:2023-12-02 10:49:19 25 4
gpt4 key购买 nike

我正在尝试使用 here 中的代码创建和使用字典:

import Data.List (lookup)

insert :: Eq a => (a,b) -> [(a,b)] -> [(a,b)]
insert (a,b) [] = [(a,b)]
insert (a,b) ((c,d):rest) = if a == c
then (a,b) : rest
else (c,d) : insert (a,b) rest

dict :: [(String, String)]
dict = [("", "")]

main = do
insert ("onekey", "onevalue") dict
print dict
print $ lookup "onekey" dict

但我收到以下错误:
$ runghc rndict.hs

rndict.hs:22:1: error:
• Couldn't match expected type ‘IO t0’ with actual type ‘[()]’
• In the expression: main
When checking the type of the IO action ‘main’

rndict.hs:24:9: error:
• Couldn't match type ‘IO’ with ‘[]’
Expected type: [()]
Actual type: IO ()
• In a stmt of a 'do' block: print dict
In the expression:
do { insert ("onekey", "onevalue") dict;
print dict;
print $ lookup "onekey" dict }
In an equation for ‘main’:
main
= do { insert ("onekey", "onevalue") dict;
print dict;
print $ lookup "onekey" dict }

rndict.hs:25:9: error:
• Couldn't match type ‘IO’ with ‘[]’
Expected type: [()]
Actual type: IO ()
• In a stmt of a 'do' block: print $ lookup "onekey" dict
In the expression:
do { insert ("onekey", "onevalue") dict;
print dict;
print $ lookup "onekey" dict }
In an equation for ‘main’:
main
= do { insert ("onekey", "onevalue") dict;
print dict;
print $ lookup "onekey" dict }

有什么问题,在 Haskell 中使用字典的正确方法是什么?

最佳答案

您需要使用 let将新字典绑定(bind)到名称:

import Data.List (lookup)

insert :: Eq a => (a,b) -> [(a,b)] -> [(a,b)]
insert (a,b) [] = [(a,b)]
insert (a,b) ((c,d):rest) = if a == c
then (a,b) : rest
else (c,d) : insert (a,b) rest

dict :: [(String, String)]
dict = [("", "")]

main = do
let d = insert ("onekey", "onevalue") dict
print d
print $ lookup "onekey" d

您询问插入多个元素。为此,您可以使用折叠来编写一个名为 insertMany 的函数。 .您可能应该使用 foldl'但我会把它作为一个练习来找出原因。
import Data.List (lookup)

insert :: Eq a => (a,b) -> [(a,b)] -> [(a,b)]
insert (a,b) [] = [(a,b)]
insert (a,b) ((c,d):rest) = if a == c
then (a,b) : rest
else (c,d) : insert (a,b) rest

insertMany :: Eq a => [(a,b)] -> [(a,b)] -> [(a,b)]
insertMany elements dict =
foldl (flip insert) dict elements

dict :: [(String, String)]
dict = [("", "")]

main = do
let d = insert ("onekey", "onevalue") dict
print d
print $ lookup "onekey" d
print $ insertMany [("onekey", "newvalue"), ("anotherkey", "anothervalue")]
dict

关于dictionary - 为什么这个字典功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56220990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com