在 Racket 中创建附加函数 [英] Creating an append function in Racket

查看:22
本文介绍了在 Racket 中创建附加函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ISL 中,您将如何创建一个 递归 append 函数,该函数接受两个列表并返回具有最高位置元素的第一个列表的所有最高位置元素的列表第二个列表(不使用 lambdaappend)?

In ISL, how would you create a recursive append function that takes two lists and returns a list of all highest position elements of the first list with the highest position elements of the second list (without using lambda or append)?

基本上一个可以用于这些检查的函数期望:

Basically a function that would hold for these check expects:

(check-expect (append-test '(a b c) '(d e f g h))  (list 'a 'b 'c 'd 'e 'f 'g 'h))
(check-expect (append-test '() '(7 2 0 1 8 3 4)) (list 7 2 0 1 8 3 4))

我觉得它肯定会使用map,因为这是我们最近一直关注的.这是我所拥有的,确实有效,但我想知道是否有办法使用 map、foldr、foldl、filter 或类似的东西来简化它.

I feel like it would definitely use map, since that's what we've been focusing on lately. Here's what I have, which does work, but I was wondering if there was a way to simplify this with map, foldr, foldl, filter, or something like that.

这是我目前所拥有的:

(define (append-test lst1 lst2)
  (cond
    [(and (empty? lst1)(empty? lst2)) '()]
    [(empty? lst1) lst2]
    [(empty? lst2) lst1]
    [else (cons (first (first (list lst1 lst2)))
                (append-test (rest lst1) lst2))]))

推荐答案

比这简单得多.

(define (append-test lhs rhs)
  (if (empty? lhs)
      rhs
      (cons (first lhs) (append-test (rest lhs) rhs))))

这篇关于在 Racket 中创建附加函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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