本文介绍了在LISP中使用"ash"执行二进制搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我现在正在阅读《 Lisp之地》,事实证明Lisp与我所见过的其他编程语言完全不同.

So, I'm reading Land of Lisp now, and Lisp is turning out to be quite different than other programming languages that I've seen.

无论如何,这本书提供了一些我们打算输入CLISP REPL的代码:

Anyways, the book provides some code that we're meant to enter into the CLISP REPL:

(defparameter *small* 1)
(defparameter *big* 100)

(defun guess-my-number ()
    (ash (+ *small* *big*) -1))

(defun smaller ()
    (setf *big* (1- (guess-my-number)))
    (guess-my-number))

(defun bigger ()
    (setf *small* (1+ (guess-my-number)))
    (guess-my-number))

现在,基本目标是创建一个猜数字游戏,其中用户/玩家选择一个数字,然后计算机尝试猜数字.它通过让玩家报告计算机猜测的号码是高于还是低于该玩家的号码来执行二进制搜索",以找到该玩家的号码.

Now, the basic goal is to create a number guessing game wherein the user/player chooses a number, and then the computer tries to guess the number. It performs a "binary search", to find the player's number, by having the player report whether the computer-guessed number is higher or lower than the player's number.

我对ash函数有些困惑.据我了解,这对于二进制搜索至关重要,但我不确定为什么.这本书在某种程度上解释了它的作用,但是有点令人困惑.

I'm a little bit confused about the ash function. It's my understanding that this is vital to the binary search, but I'm not really sure why. The book somewhat explains what it does, but it's a little confusing.

ash函数的作用是什么?为什么要传递添加到*big*-1*small*参数?它是如何工作的?它对二进制搜索有什么作用?

What does the ash function do? Why is it passed the parameters of *small* added to *big* and -1? How does it work? What purpose does it serve to the binary search?

推荐答案

Google为您提供此页面,其中说明ash是算术移位运算.因此,(ash x -1)x右移一位,从而将其整数减半.

Google gives you this page which explains that ash is an arithmetic shift operation. So (ash x -1) shift x by one bit to the right, so gives its integer half.

这篇关于在LISP中使用"ash"执行二进制搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 06:54