SML-获取列表索引 [英] SML - Get Indices of List

查看:41
本文介绍了SML-获取列表索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个程序,该程序将'+'或'-'附加到列表的元素上,具体取决于该元素的索引是奇数还是偶数(即交替和列表).

但是,我很难确定每个元素的索引是什么.我的代码相信应该使用 if语句 mod

附加正确的符号

 有趣的交替([])= 0|交替(l)=如果List.nth(l,hd(l))mod 2 == 0,则'+'@ hd(l)@alternating(tl(l))否则'-'@ hd(l)@alternating(tl(l)) 

但是, List.nth(l,hd(l))总是返回第二个索引处的元素,而不是第一个索引.

解决方案

当您真的只想对整数求反,以便可以将它们传递给某种求和时,我会否定参数,如果它很奇怪.使用相互递归可以做到这一点,而无需任何明确的索引簿记:

 有趣的选择l =让有趣的alter1 [] = []|交替1(x :: xs)=(〜x)::交替2 xs和alternate2 [] = []|交替2(x :: xs)= x ::交替1 xs在备用1升结尾 

它的工作原理如下:

 -备用[1,2,3,4];val it = [〜1,2,〜3,4]:整数列表 

我强烈建议您使用模式匹配,而不要使用 hd .

编辑讨论 hd

根据经验,如果您需要 hd ,则可能还需要 tl . hd 是部分函数-如果您的列表为空,它将抛出 Empty .如果您进行模式匹配,则可以方便地在此处获取列表开头和结尾的变量,并且可以直观地看到需要处理空列表的提示.海事组织(IMO)在审美上更令人愉悦:

  fun foo [] = ...|foo(x :: xs)= ... 

比同等

  fun foo l =如果为空l然后 ...其他(hd l)...(tl l) 

换句话说,您将获得简短,简洁的代码,并带有自动提醒以使其正确无误.双赢.据我所知,以其他方式进行操作并没有明显优势.当然,您可能会发现自己处在这种情况下,即您知道列表将至少包含一个元素,而您无需执行其他任何操作.您仍然必须考虑所给的情况,但这是一个很好的经验法则.

I'm working on a program that appends either a '+' or '-' to an element of a list, depending on whether the index of that element is odd or even (i.e an alternating sums list).

However, I'm having trouble identifying what the index of each element is. I have code that I believe should append the correct symbol, using if statements and mod

fun alternating([]) = 0
  | alternating(l) = 
        if List.nth(l,hd(l)) mod 2 == 0 then '+'@hd(l)@alternating(tl(l))
        else '-'@hd(l)@alternating(tl(l))

However, List.nth(l,hd(l)) always returns the element at the second index, not the first.

解决方案

On the off chance that you really just want to negate integers them so you can pass them into some kind of summation, I would just negate the argument if it's odd. Using mutual recursion one can do it without any explicit index bookkeeping:

fun alternate l =
   let
       fun alternate1 []      = []
         | alternate1 (x::xs) = (~x) :: alternate2 xs
       and alternate2 []      = []
         | alternate2 (x::xs) =   x  :: alternate1 xs
   in
       alternate1 l
   end

It works like so:

- alternate [1,2,3,4];
val it = [~1,2,~3,4] : int list

I would strongly encourage you to use pattern matching instead of hd.

Edit discussing hd

As a rule of thumb, if you need hd you probably need tl as well. hd is a partial function--it's going to throw Empty if your list is empty. If you pattern match, you conveniently get variables for the head and tail of the list right there, and you get a visual reminder that you need to handle the empty list. It's more aesthetically pleasing, IMO, to see:

fun foo []      = ...
  | foo (x::xs) = ...

than the equivalent

fun foo l = 
  if null l 
    then ... 
    else (hd l) ... (tl l)

In other words, you get shorter, cleaner code with an automatic reminder to make it correct. Win/win. To my knowledge there's no significant advantage to doing it the other way. Of course, you may find yourself in a situation where you know the list will have at least one element and you don't need to do anything else. You still have to consider the cases you're given, but it's a good rule of thumb.

这篇关于SML-获取列表索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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