gpt4 book ai didi

purescript - 如何显示 PureScript 列表中的每个项目

转载 作者:行者123 更新时间:2023-12-04 14:41:27 25 4
gpt4 key购买 nike

纯脚本列表中是否有 foreach 方法? foreach 方法获取列表的每一项并返回一个单位。

它是打印列表中每个项目的好方法。

编辑:我正在尝试下面建议的 traverse 方法,但出现错误

import Data.Traversable (traverse)
removeDuplicate :: AddressBook -> AddressBook
removeDuplicate = nubBy (\a b -> a.firstName == b.firstName && a.lastName == b.lastName)
let dedup = removeDuplicate addressBook
traverse (\a -> log (showEntry a)) dedup

Compiling Main
Error found:
in module Main
at src/Main.purs line 73, column 3 - line 73, column 49

Could not match type

List Unit

with type

Unit


while trying to match type Eff
( console :: CONSOLE
| t1
)
t2
with type Eff
( console :: CONSOLE
| e0
)
Unit
while checking that expression (discard (logShow ((...) addressBook))) (\__unused ->
(discard (...)) (\__unused ->
...
)
)
has type Eff
( console :: CONSOLE
| e0
)
Unit
in value declaration main

where e0 is a rigid type variable
bound at line 63, column 8 - line 78, column 38
t2 is an unknown type
t1 is an unknown type

See https://github.com/purescript/documentation/blob/master/errors/TypesDoNotUnify.md for more information,
or to contribute content related to this error.

最佳答案

您可以使用 for_ 它允许您对 Foldable 的每个项目执行“应用操作” ( ListArrayFoldable 实例)并忽略此操作结果:

module Main where

import Control.Monad.Eff.Console (logShow)
import Data.Foldable (for_)
import Prelude

main = do
-- I'm using an Array here for simplicity, but the same goes for List
let l = [1, 2, 3, 4]

for_ l \i → logShow i
List还有 Show实例( second on the instances list )(还有数组 -
last on the instances list ) 以便您可以使用 logShow 打印它们如果它们包含类型为 Show 的值,则直接使用实例。

关于purescript - 如何显示 PureScript 列表中的每个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48777021/

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