English 中文(简体)
第一个大( 普通) lisp 程序 - > 随机不按预期工作
原标题:First larger (common) lisp program -> random not working as expected

为了好玩,我在Python做了一个模拟 “monty Hall” > “mony Hall problem” “monty Hall problem” 。后来,我与Lua进行了实验,决定在Lua再写一次,看看它如何比较。这是一个相当有趣的经历,尽管程序看起来非常相似(Lua版本略短 ) 。 最近,我开始与 CL 进行实验, 并想再次做同样的事情。 然而, 当我运行它时, 它没有如预期的那样。 出于某种原因, 不稳定的玩家( 他应该获得66%的获胜机会) 几乎和天球手一样得分(50%的赢机会 ) 。

有人能给我一个提示吗?这不是一个家庭作业或类似的东西,只是我第一次尝试与 CL 一起写一个更大的程序。除了上面描述的问题的提示外,我还欢迎就如何改进我的风格提出建议。我想它还是相当漂亮的Python-sish (它或多或少是一个直接的翻译 ) 。

(defun choose-one (l)
  "Ramdomly chooses one element of the given list"
  (nth (random (length l)) l))

(defun remove-one (l)
  "Randomly removes one element of the given list"
  (remove (choose-one l) l))

(defun naive-player (initial-choice possible-choices)
  "The naive player randomly picks one choice. Should have a 50% chance to win."
  initial-choice ;keep compiler happy
  (choose-one possible-choices))

(defun stubborn-player (initial-choice possible-choices)
  "The stubborn player sticks with his initial choice. Should have a 33% chance to win."
  possible-choices ;keep compiler happy
  initial-choice)

(defun erratic-player (initial-choice possible-choices)
  "The erratic player will always change his choice. Should have a 66% chance to win."
  (choose-one (remove initial-choice possible-choices)))

(defun host-offer (prize possible-choices)
  "The host reveals one wrong choice."
  (let ((remaining (remove prize possible-choices)))
    (remove (choose-one remaining) possible-choices)))

(defun one-game (playerfn choices)
  "Simulates a single game with the given player. Evaluates to T if the player won."
  (let ((prize (choose-one choices))
        (player-choice (choose-one choices)))
    (eq (funcall playerfn player-choice (host-offer prize choices)) prize)))


(defun multiple-games (num-games)
  "Simulates the given number of games with all players. Evaluates to a result list."
  (let ((choices  (door_a door_b door_c))
        (naive-score 0)
        (stubborn-score 0)
        (erratic-score 0))
    ;(progn
      (dotimes (i num-games)
        ;(progn
          (if (one-game # naive-player choices)
              (incf naive-score))
          (if (one-game # stubborn-player choices)
              (incf stubborn-score)) 
          (if (one-game # erratic-player choices)
              (incf erratic-score)));)
      (list 
       (list  naive-player naive-score) 
       (list  stubborn-player stubborn-score)
       (list  erratic-player erratic-score))));)

;; Run simulation and display results
(defparameter *num-games* 10000)
(format *standard-output* "--- Monty Hall ---~%")
(format *standard-output* "Simulating ~D games...~%" *num-games*)
(let ((result (multiple-games *num-games*)))
  (format *standard-output* "~{~{~A score: ~D~}~%~}" result))

产出(例如):

--- Monty Hall ---
Simulating 10000 games...
NAIVE-PLAYER score: 5014
STUBBORN-PLAYER score: 3333
ERRATIC-PLAYER score: 4968
最佳回答

主机报价函数错误。 在蒙蒂厅的问题中, 定义的东道主永远不会打开玩家选择的门, 但您的程序没有考虑到这一点。 如果已经固定了, 程序返回预期的结果 。

问题回答

暂无回答




相关问题
Lisp code called from Java

Long story: I am doing a project for my functional programing class, and I thought of writing an AI controller in Lisp, for the Mario AI competition. I was looking over frameworks/libraries/ways of ...

Emacs, Zen-Coding mode, and Putty

I use emacs via Putty and since Putty doesn t send certain key combinations to the remote console I generally need to re-bind them to other key combinations. After installing the amazing Zen-Coding ...

In Which Cases Is Better To Use Clojure? [closed]

I develop in Lisp and in Scheme, but I was reading about Clojure and then I want to know, in which cases is better to use it than using Lisp or Scheme? Thanks

lambda-gtk negative pointer

I was trying to write my own put-pixel on (Gdk) pixbuf in Lisp. When I finally realized how I can operate on C pointers in CL, new obstacle came along - (gdk:pixbuf-get-pixels pb) returns me negative ...

Is there a common lisp package naming convention?

I have created some of my own user packages and have run into a name clash. In Java, the naming convention is to use your domain name in the package name: e.g. import com.example.somepackage;. Are ...

SOAP request from within an AutoLISP/AutoCAD macro

We have built a webservice for a client that uses AutoCAD. They have a macro that runs in AutoCAD that builds a SOAP request. But they have not figured out how to actually send() the soap request to ...

热门标签