案例陈述和模式匹配 [英] Case Statements and Pattern Matching

查看:104
本文介绍了案例陈述和模式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SML中编写了一个任务,我做了一些练习题,我觉得我错过了一些东西 - 我觉得我正在使用太多的 case 语句。以下是我正在做的以及我遇到问题时的问题陈述:


  1. 编写一个函数all_except_option,它接受一个字符串和一个字符串列表。如果字符串不在列表中,则返回NONE,否则返回SOME lst,其中lst与参数列表类似,但字符串不在其中。

      fun all_except_option(str:string,lst:string list)= 
    case bst
    [] => NONE
    | x :: xs => case
    的情况same_string(x,str)true =>有些xs
    | false => case
    的all_except_option(str,xs)NONE => NONE
    |有些y => SOME(x :: y)


  2. 编写一个get_substitutions1函数, (字符串列表,替换)和一个字符串s,并返回一个字符串列表。结果中的所有字符串在某些列表中也有s,但s本身不应该在结果中。

      fun get_substitutions1(lst:string list list,s:string)= 
    case bst
    [ ] => []
    | x :: xs => case
    的all_except_option(s,x)NONE => get_substitutions1(xs,s)
    |有些y => y @ get_substitutions1(xs,s)


-
same_string 是一个提供的函数,
fun same_string(s1:string,s2:string)= s1 = s2

解决方案

首先,我将开始在函数定义
中使用模式匹配,一个顶级案例陈述。它基本上归结为脱糖后的
同样的东西。除非严格需要,否则我将摆脱显式类型注释:

$ p $ fun all_except_option(str,[])= NONE
| all_except_option(str,x :: xs)=
case
的same_string(x,str)true =>有些xs
| false => case
的all_except_option(str,xs)NONE => NONE
|有些y => SOME(x :: y)

get_substitutions1([],s)= []
| get_substitutions1(x :: xs,s)=
案例all_except_option(s,x)
NONE => get_substitutions1(xs,s)
|有些y => y @ get_substitutions1(xs,s)

如果速度不重要,那么您可以合并这两个第一个函数中的例子:

  fun all_except_option(str,[])= NONE 
|
(true,_)=>的all_except_option(str,x :: xs)=
case(same_string(x,str),all_except_option(str,xs))有些xs
| (假,无)=> NONE
| (false,SOME y)=> SOME(x :: y)

但由于您使用append(@),所以在第二个函数,既然它不是
尾递归,我不认为它是你的主要关注点。请记住,
追加是潜在的邪恶,你应该几乎总是使用串联(和
,然后在返回时反转你的结果),并在可能的时候使用尾递归(它始终是
)。



如果您真的喜欢显式类型注释,那么您可以这样做:

  val rec all_except_option:string * string list  - >字符串列表选项= 
fn(str,[])=> NONE
| (str,x :: xs)=>
case(same_string(x,str),all_except_option(str,xs))
(true,_)=>有些xs
| (假,无)=> NONE
| (false,SOME y)=> SOME(x :: y)


val rec get_substitutions1:字符串列表列表*字符串 - >字符串列表=
fn([],s)=> []
| (x :: xs,s)=>
case all_except_option(s,x)
NONE => get_substitutions1(xs,s)
|有些y => y @ get_substitutions1(xs,s)

但这只是我的首选方式,如果我真的需要添加类型注释。



顺便问一下,为什么你有 same_string 函数?您可以直接进行比较。除非你打算在某些时候用一些特殊的逻辑来交换它,否则使用辅助函数是很奇怪的。但是你的函数名称并不能解决这个问题。


I'm coding in SML for an assignment and I've done a few practice problems and I feel like I'm missing something- I feel like I'm using too many case statements. Here's what I'm doing and the problem statements for what I'm having trouble with.:

  1. Write a function all_except_option, which takes a string and a string list. Return NONE if the string is not in the list, else return SOME lst where lst is like the argument list except the string is not in it.

    fun all_except_option(str : string, lst : string list) =
      case lst of 
       [] => NONE
      | x::xs => case same_string(x, str) of
                   true => SOME xs
                 | false => case all_except_option(str, xs) of
                              NONE => NONE
                            | SOME y=> SOME (x::y)  
    

  2. Write a function get_substitutions1, which takes a string list list (a list of list of strings, the substitutions) and a string s and returns a string list. The result has all the strings that are in some list in substitutions that also has s, but s itself should not be in the result.

    fun get_substitutions1(lst : string list list, s : string) = 
      case lst of
        [] => []
      | x::xs => case all_except_option(s, x) of
                     NONE => get_substitutions1(xs, s)
                    | SOME y => y @ get_substitutions1(xs, s)
    

- same_string is a provided function, fun same_string(s1 : string, s2 : string) = s1 = s2

解决方案

First of all I would start using pattern matching in the function definition instead of having a "top-level" case statement. Its basically boils down to the same thing after de-sugaring. Also I would get rid of the explicit type annotations, unless strictly needed:

fun all_except_option (str, []) = NONE
  | all_except_option (str, x :: xs) =
    case same_string(x, str) of
      true  => SOME xs
    | false => case all_except_option(str, xs) of
                 NONE   => NONE
               | SOME y => SOME (x::y)

fun get_substitutions1 ([], s) = []
  | get_substitutions1 (x :: xs, s) =
    case all_except_option(s, x) of
      NONE   => get_substitutions1(xs, s)
    | SOME y => y @ get_substitutions1(xs, s)

If speed is not of importance, then you could merge the two cases in the first function:

fun all_except_option (str, []) = NONE
  | all_except_option (str, x :: xs) =
    case (same_string(x, str), all_except_option(str, xs)) of
      (true, _)       => SOME xs
    | (false, NONE)   => NONE
    | (false, SOME y) => SOME (x::y)

But since you are using append (@), in the second function, and since it is not tail recursive, I don't believe that it your major concern. Keep in mind that append is potential "evil" and you should almost always use concatenation (and then reverse your result when returning it) and tail recursion when possible (it always is).

If you really like the explicit type annotations, then you could do it like this:

val rec all_except_option : string * string list -> string list option  =
 fn (str, []) => NONE
  | (str, x :: xs) =>
    case (same_string(x, str), all_except_option(str, xs)) of
      (true, _)       => SOME xs
    | (false, NONE)   => NONE
    | (false, SOME y) => SOME (x::y)


val rec get_substitutions1 : string list list * string -> string list =
 fn ([], s) => []
  | (x :: xs, s) =>
    case all_except_option(s, x) of
      NONE   => get_substitutions1(xs, s)
    | SOME y => y @ get_substitutions1(xs, s)

But that is just my preferred way, if I really have to add type annotations.

By the way, why on earth do you have the same_string function? You can just do the comparison directly instead. Using an auxilary function is just wierd, unless you plan to exchange it with some special logic at some point. However your function names doesn't sugest that.

这篇关于案例陈述和模式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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