编写一个方案功能 [英] Writing a scheme function

查看:117
本文介绍了编写一个方案功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何编写一个函数,该函数同时采用一个评分函数(我已经写过)和一串字符串作为输入(我对如何编写感到困惑),并返回一个修改后的列表成对的字符串,其中返回的列表应该包含来自输​​入的所有最佳字符串对,根据输入函数进行评分。



输入示例:



$ p $ '((hello )(h_e_llobl_o__w))

输出示例:

 ((hellob_low)(hello_b_l_ow)(hello_blow))

函数获取如上所示的字符串对列表。它也承担一项功能。它使用这个函数作为评估字符串对列表的手段。然后它返回一个包含所有匹配得分最高的字符串对的字符串对的列表,这些对匹配得分最高的字符串是基于它给予评估的函数的。换句话说,((hellob_low)(hello_b_l_ow)(hello_blow))的分数都是-3,但是(h_e_llobl_o__w))得分为-12,因此它从列表中删除。



计算对齐函数:
$ b $ ($ char $ char $ char $ 2
$($($($($($($($($ char =?char1#\_)(char =?#\_ char2))-2)
(else -1)))

(define(alignment-score s1 s2 )
(定义最小长度(min(字符串长度s1)(字符串长度s2)))
(let循环((score 0)(index 0))
(if =索引最小长度)
得分
(循环(+得分(char-scorer(string-ref s1 index)(string-ref s2 index)))(+ index 1)))))


解决方案

我会将操作分成两步。


  1. 计算最大分数,这是一个可以做到的功能。 $ p> (定义(得到最大分数第一分数函数)
    (应用最大值(map(lambda(x)(得分函数(汽车x)(cadr x)))lst)))


  2. 通过选择符合最高分数的项目并删除其余项目来筛选列表。

     (define(get-maximum-score-items lst scoring-func)

    (define max-score(get-maximum-score lst scoring-func))

    (define(helper in out)
    (if(null? in)
    out
    (if(eq?max-score(calor in)(cadar in))
    (helper(cdr in)(append out(list(car in))))
    (helper(cdr in)out))))

    (helper lst'())


现在获得结果。

<$ (hellob_low)(hello_b_l_ow)(hello)(打印
(get-maximum-score-items
' _blow)(helloblow)(h_e_llobl_o__w))
alignment-score))


How do I write a function that takes both a scoring function (which I've written already) and a list of pairs of strings as input (which I'm confused on how to write), and returns a modified list of pairs of strings, where the returned list should contain all the optimal string pairs from the input, scored according to the input function.

Example input:

'( ("hello" "b_low") ("hello_" "b_l_ow") ("hello" "_blow") ("hello" "blow") ("h_e_llo" "bl_o__w") )

Example output:

( ("hello" "b_low") ("hello_" "b_l_ow") ("hello" "_blow") )

the function takes a list of pairs of strings as shown above. It also takes in a function. It uses this function that it takes in as a means to evaluate the list of pairs of strings. It then returns a list of pairs of strings containing all of the pairs of strings that had the highest match-score based on the function it was given to evaluate them with. In other words, (("hello" "b_low") ("hello_" "b_l_ow") ("hello" "_blow")) all had the same score of -3, but ("h_e_llo" "bl_o__w")) has a score of -12, thus it is dropped from the list.

The functions to compute alignemt:

(define (char-scorer char1 char2)
  (cond 
    ((char=? char1 char2) 2)
    ((or (char=? char1 #\_ ) (char=? #\_ char2)) -2)
    (else -1)))

(define (alignment-score s1 s2)
  (define min-length (min (string-length s1) (string-length s2)))
  (let loop ((score 0) (index 0))
    (if (= index min-length)
      score
      (loop (+ score (char-scorer (string-ref s1 index) (string-ref s2 index)))(+ index 1))))) 

解决方案

I would divide the operation into two steps.

  1. Compute the maximum score. Here's a function that can do that.

    (define (get-maximum-score lst scoring-func)
     (apply max (map (lambda (x) (scoring-func (car x) (cadr x))) lst)))
    

  2. Filter the list by selecting the items that match the maximum score and drop the rest.

    (define (get-maximum-score-items lst scoring-func)
    
      (define max-score (get-maximum-score lst scoring-func))
    
      (define (helper in out)
        (if (null? in)
          out
          (if (eq? max-score (scoring-func (caar in) (cadar in)))
            (helper (cdr in) (append out (list (car in))))
            (helper (cdr in) out))))
    
      (helper lst '())
      )
    

Now get the result.

(print
 (get-maximum-score-items
  '(("hello" "b_low") ("hello_" "b_l_ow") ("hello" "_blow") ("hello" "blow") ("h_e_llo" "bl_o__w"))
     alignment-score))

这篇关于编写一个方案功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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