方案功能 [英] Scheme function

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

问题描述

 (define(ys lis)
(cond)
((null?lis)'())
((等于?s(car lis))lis)
(else(ys(cdr lis)))))

它会运行,但我不确定它在做什么或试图做什么。
是否需要列表进行排序?我正在使用DrRacket来运行它。我从来没有见过
之前的计划,任何帮助将不胜感激。

解决方案

这是一个搜索函数,给定一个值并且列表查找列表中的值。如果找到它,则返回从找到元素的位置开始的列表部分。如果该值不在列表中,它将返回一个空列表。让我们一步一步看看它是如何工作的:

 ;定义一个名为`y`的函数,它接收
;作为参数一个值`s`和一个列表`lis`
(define(ys lis)
;我们要测试三个可能的条件
(cond
;如果列表为空
((null?lis)
;然后我们没有找到该值,返回空列表
'())
;如果第一个元素在(等于?s(car lis))
;然后返回列表直到这个点
lis)
;否则
(else
;继续查看列表的其余部分,通过反复查看
(ys(cdr lis)))))

为了看到这个函数的作用,让我们用一些值来测试它:

 ;使用存在于列表
(y 3'(1 2 3 4 5))
中的值调用该函数;看看如何从找到值的地方开始返回列表
=> '(3 4 5)

;使用列表
(y 7'(1 2 3 4 5))
中不存在的值调用该函数;一个空的列表被返回
=> '()

为了真正理解这是如何工作的,您必须意识到在最后一行我们称之为函数本身的函数 - 这是递归的魔力!此外,它还会帮助您了解每个使用的原始操作都在做什么,点击每个链接以阅读文档,我简化并总结了关键点:$ b​​
$ b


  • define :给一个值赋一个名字(特别是它可以是一个函数)
  • cond :针对不同的条件进行测试,根据哪些条件为真来执行一些表达式。

  • 等于? :比较两个对象是否相等

  • car :获取列表中的第一个元素

  • cdr :获取列表中其余的元素


I am trying to interpret what this scheme function does:

 (define (y s lis)
    (cond
      ((null? lis) '() )
      ((equal? s (car lis)) lis)
      (else (y s (cdr lis)))))

It runs but I am not exactly sure what it is doing or trying to do. Does it need a list to sort or something? I am using DrRacket to run it. I have never seen scheme before and any help would be greatly appreciated.

解决方案

It's a search function, that given a value and a list looks for the value in the list. If it finds it, returns the part of the list starting at the point where the element was found. If the value isn't in the list, it returns an empty list. Let's see how this works, step by step:

; define a function called `y` that receives
; as parameters a value `s` and a list `lis`
(define (y s lis)
  ; we're going to test three possible conditions
  (cond
    ; if the list is empty    
    ((null? lis)
     ; then we didn't find the value, return the empty list
     '())
    ; if the first element in the list equals the `s` value
    ((equal? s (car lis))
     ; then return the list up to this point
     lis)
    ; otherwise
    (else
     ; keep looking in the rest of the list, by recurring over it
     (y s (cdr lis)))))

To see the function in action, let's test it with some values:

; call the function with a value that is present in the list
(y 3 '(1 2 3 4 5))
; see how the list is returned starting at the point where the value was found
=> '(3 4 5)

; call the function with a value that is not present in the list
(y 7 '(1 2 3 4 5))
; an empty list is returned
=> '()

To really, really understand how this works, you have to realize that in the last line of the function we're calling the function itself - it's the magic of recursion! Also, it'll help you to understand what each one of the primitive operations used is doing, click on each link to read the documentation, I'm simplifying and summarizing the key points:

  • define: give a name to a value (in particular, it can be a function)
  • cond: test for different conditions, execute some expressions depending on which condition was true
  • null?: test to see if a list is empty
  • equal?: compare two objects for equality
  • car: get the first element in a list
  • cdr: get the rest of the elements in a list

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

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