如何返回元素个数? [英] How to return the number of elements?

查看:90
本文介绍了如何返回元素个数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写一个函数,该函数将整数列表作为参数&返回列表中小于1的整数的数目。到目前为止,我拥有的一个函数只是返回列表中有多少个整数。我不确定应该在哪里/如果要放置if语句和计数器以仅返回小于1的整数。

I have to write a function that takes a list of integers as a parameter & returns the number of integers from the list that are less than 1. What I have so far is a function that just returns how many integers in the list. I am not sure where/if I'm supposed to put a if statement and counter to only return how many integers are less than 1.

-export([num/1]).

num([]) -> 0 ;
num(L) -> num(L,0).

num([],Len) -> Len;
num([_|T],Len) ->
    num(T,Len+1).


推荐答案

您的代码已经差不多了。要学习的关键技能: guard

Your code is almost there. Key skill to learn: guard

-export([num/1]).

num([]) -> 0;
num(NUMS) ->
        num(NUMS, 0).

num([H|L], Count) when H < 1 ->  %% use of guard
        num(L, Count+1);
num([_|L], Count) ->
        num(L, Count);
num([], Count) ->
        Count.

这篇关于如何返回元素个数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆