小编use*_*183的帖子

Haskell类型错误与where

type NI = Int
type Age = Int
type Balance = Int
type Person = (NI, Age, Balance) 
type Bank = [Person]


sumAllAccounts :: NI -> Bank -> Int
sumAllAccounts n l = filter niMatch l
    where niMatch n (a,_,_) 
        | n == a    =   True
        | otherwise =   False
Run Code Online (Sandbox Code Playgroud)

当我运行此功能时,我得到一个类型错误

couldnt match type (Person, t0, t1) -> Bool with Bool
Run Code Online (Sandbox Code Playgroud)

但是当我把它自己的功能放在哪里时它起作用

personNIMatchs :: NI -> Person -> Bool
personNIMatchs n (a,_,_) 
    | n == a    =   True
    | otherwise …
Run Code Online (Sandbox Code Playgroud)

haskell types

5
推荐指数
1
解决办法
99
查看次数

总结一个元组列表

我有一个包含整数的元组列表.

[(123,123,123),(123,123,123),(123,123,123)]
Run Code Online (Sandbox Code Playgroud)

我想得到列表中所有第二个值的总和,例如;

[(_,123,_),(_,123,_),(_,123,_)]
Run Code Online (Sandbox Code Playgroud)

123 + 123 + 123

我想我应该使用折叠,但我不确定

haskell tuples sum

-1
推荐指数
1
解决办法
1358
查看次数

标签 统计

haskell ×2

sum ×1

tuples ×1

types ×1