如何获得列表方案和序言的第一个,中间和最后一个元素? [英] How to get the first, middle and last element of a list scheme and prolog?

查看:76
本文介绍了如何获得列表方案和序言的第一个,中间和最后一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Scheme和Prolog中编写一个函数,该函数返回列表的第一,中间和最后一项.例如find([4,5,8,7,9],L), L = [4,8,9].

I am trying to write a function in Scheme and Prolog that returns first, middle and last item of a list. E.g., find([4,5,8,7,9],L), L = [4,8,9].

我想出了这段用于Scheme语言的代码,但是我对Prolog并不陌生,也不了解很多,那么如何在Prolog中获得相同的结果?

I came up with this piece of code for Scheme language, but I am new to Prolog and don't know much, so how I can get the same result in Prolog?

(define (frst L)
   (car L))

(define (last L)
   (if (null? (cdr L))
       (car L)
       (last (cdr L))))

(define (nth L x)
   (if (= x 1)
       (car L)
       (nth (cdr L) (- x 1))))

(define (firstMidLast L)
   (list (frst L)
         (nth L (ceiling (/ (length L) 2)))
         (last L)))

推荐答案

这是另一种方法!

  • 诀窍"是要以两种不同的速度走下相同的列表.
  • 第一个自变量索引使目标list_first_mid_last(+,?,?,?)保持确定性.
  • The "trick" is to walk down the same list at two different speeds.
  • First argument indexing keeps goals list_first_mid_last(+,?,?,?) deterministic.

我们这样定义list_first_mid_last/4:

list_first_mid_last([E|Es],E,M,L) :-
    ahead_of_mid_last([E|Es],[E|Es],M,L).

ahead_of_mid_last([],[M|_],M,M).
ahead_of_mid_last([F|Fs],Es,M,L) :-
   more_ahead_of_mid_last(Fs,F,Es,M,L).

more_ahead_of_mid_last([],L,[E|_],E,L).
more_ahead_of_mid_last([F|Fs],_,Es,E,L) :-
   evenmore_ahead_of_mid_last(Fs,F,Es,E,L).

evenmore_ahead_of_mid_last([],L,[E|_],E,L).
evenmore_ahead_of_mid_last([F|Fs],_,[_|Es],M,L) :-
    more_ahead_of_mid_last(Fs,F,Es,M,L).

让我们运行一些查询,并将Prolog 1 和Scheme 2 结果并排放置!

Let's run a few queries and put Prolog1 and Scheme2 results side-by-side!


%  Prolog                                     % ; Scheme
?- list_first_mid_last([1],F,M,L).            % > (firstMidLast `(1))
F = M, M = L, L = 1.                          % (1 1 1)
                                              %
?- list_first_mid_last([1,2],F,M,L).          % > (firstMidLast `(1 2))
F = M, M = 1, L = 2.                          % (1 1 2)
                                              %
?- list_first_mid_last([1,2,3],F,M,L).        % > (firstMidLast `(1 2 3))
F = 1, M = 2, L = 3.                          % (1 2 3)
                                              %
?- list_first_mid_last([1,2,3,4],F,M,L).      % > (firstMidLast `(1 2 3 4))
F = 1, M = 2, L = 4.                          % (1 2 4)
                                              %
?- list_first_mid_last([1,2,3,4,5],F,M,L).    % > (firstMidLast `(1 2 3 4 5))
F = 1, M = 3, L = 5.                          % (1 3 5)
                                              %
?- list_first_mid_last([1,2,3,4,5,6],F,M,L).  % > (firstMidLast `(1 2 3 4 5 6))
F = 1, M = 3, L = 6.                          % (1 3 6)
                                              %
?- list_first_mid_last([1,2,3,4,5,6,7],F,M,L).% > (firstMidLast `(1 2 3 4 5 6 7))
F = 1, M = 4, L = 7.                          % (1 4 7)


脚注1:使用版本7.3.11(64位).
脚注2:使用解释程序 SCM 版本5e5(64位).


Footnote 1: Using swi-prolog version 7.3.11 (64-bit).
Footnote 2: Using the scheme interpreter SCM version 5e5 (64-bit).

这篇关于如何获得列表方案和序言的第一个,中间和最后一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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