首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么此字典函数不起作用

为什么此字典函数不起作用
EN

Stack Overflow用户
提问于 2019-05-20 20:25:25
回答 1查看 83关注 0票数 0

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

代码语言:javascript
复制
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

但是我得到了以下错误:

代码语言:javascript
复制
$ 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中使用字典的正确方法是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-20 20:28:02

您需要使用let将新字典绑定到一个名称:

代码语言:javascript
复制
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',但我将把它作为练习来找出原因。

代码语言:javascript
复制
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
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56220990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档