创建给定列表的子列表列表

时间:2016-11-18 15:49:52

标签: list haskell

我需要一个函数来返回所有可能的子列表的列表,而不会跳过元素,例如:子列表[1,2,3,4]应返回[[1,2,3,4],[1,2,3]等]但列表不应包含[1,2,4]。 我目前的解决方案"是

type BBPeripheral struct {
    client   *http.Client
    endpoint string
}

type BBQuery struct {
    Name string `json:"name"`
}

type BBResponse struct {
    Brand          string `json:"brand"`
    Model          string `json:"model"`
    ...
}

type Peripheral struct {
    Brand          string 
    Model          string 
    ...
}

type Service interface {
    Get(name string) (*Peripheral, error)
}

func NewBBPeripheral(config *peripheralConfig) (*BBPeripheral, error) {
    transport, err := setTransport(config)
    if err != nil {
        return nil, err
    }

    BB := &BBPeripheral{
        client:   &http.Client{Transport: transport},
        endpoint: config.Endpoint[0],
    }

    return BB, nil
}


func (this *BBPeripheral) Get(name string) (*Peripheral, error) {

    data, err := json.Marshal(BBQuery{Name: name})
    if err != nil {
        return nil, fmt.Errorf("BBPeripheral.Get Marshal: %s", err)
    }

    resp, err := this.client.Post(this.endpoint, "application/json", bytes.NewBuffer(data))
    if resp != nil {
        defer resp.Body.Close()
    }
    if err != nil {
        return nil, err
    }
    if resp.StatusCode != http.StatusOK {
        return nil, fmt.Errorf(resp.StatusCode)
    }

    var BBResponse BBResponse

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return nil, err
    }

    err = json.Unmarshal(body, &BBResponse)
    if err != nil {
        return nil, err
    }

    peripheral := &Peripheral{}

    peripheral.Model = BBResponse.Model
    if peripheral.Model == "" {
        peripheral.Model = NA
    }

    peripheral.Brand = BBResponse.Brand
    if peripheral.Brand == "" {
        peripheral.Brand = NA
    }

    return peripheral, nil
}

确实包括[1,2,4]

提前致谢

编辑:找到一个解决方案(在我朋友的帮助下)

看起来有点笨拙但是有效

>sublists :: [Integer] -> [[Integer]]
>sublists [] = [[]]
>sublists (x:xs) = [x:ys | ys <- sublists xs] ++ sublists xs

1 个答案:

答案 0 :(得分:2)

Data.List包含initstails。你想要的是尾部列表中每个成员的内容(或者可能反之亦然,但是稍后会看到为什么这种方式更好)

sublists = concatMap inits . tails

> sublists [1,2,3,4]
[[],[1],[1,2],[1,2,3],[1,2,3,4],[],[2],[2,3],[2,3,4],[],[3],[3,4],[],[4],[]]

如果您愿意,可能想要删除所有空列表:

sublists = filter (not . null) . concatMap inits . tails

或者如果您希望首先避免生成空列表:

sublists = concatMap (tail . inits) . tails

inits的结果始终以空列表开头,而tails的结果始终以空列表结束。因此tail . inits是安全的,因为tail永远不会应用于空列表;它只返回没有前导空列表的结果。 inits []只返回[[]],因此tails中的最后一个空列表将被删除。

相关问题