Page 4 of 4 FirstFirst ... 234
Results 31 to 34 of 34

Thread: [Beginner] Programming Challenge: 6

  1. #31
    Join Date
    Dec 2007
    Beans
    485
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: [Beginner] Programming Challenge: 6

    How can you subscribe to the new challenges that come out so your notified?

  2. #32
    Join Date
    Dec 2007
    Beans
    485
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: [Beginner] Programming Challenge: 6

    I think I will go through the other's without looking at what everyone else did to get caught up

    first one's pretty easy

  3. #33
    Join Date
    Apr 2006
    Beans
    265

    Re: [Beginner] Programming Challenge: 6

    Late to the party but I'll post 'em anyway.

    I found the bottle song hard to do in a 'nice' way. First you have to learn lisp and then you have to learn the language format uses as well.

    Code:
    (defun pre-format (number)
      (if (= 0 number) "no more bottles" (format nil "~r bottle~:p" number)))
    
    (loop :for numbottles :from 99 :downto 0 :do
       (format t "~@(~a~) of beer on the wall, ~:*~a of beer.~%" (pre-format numbottles))
       (if (< 0 numbottles)(format t "Take one down, pass it around, ~a of beer on the wall. ~2%"  (pre-format (1- numbottles)))
           (format t "Go to the store and buy some more, ninety-nine bottles of beer on the wall.~2%")))
    Can a lisper please tell me how I can use ~r for the numbers except for 0 where I want "no more" instead of "zero"? I got pissed off in the end and used a function outside of format. At least I have all the words of the song typed just once.
    Also couldn't work out how to do the condition for the last line of the song inside the format call and used two formats where I guess only one is needed.

    Also had a go at the number guessing game.

    Code:
    (defun make-em-guess (tellemwottodo)
      (format t "~a: " tellemwottodo)
      (or (parse-integer (read-line) :junk-allowed t)
          (make-em-guess "Not an integer. Try again")));If input is not an integer, re-call make-em-guess with error message.
    
    (do ((secretnumber (+ 1 (random 100)))
         (numberofgoes 1 (1+ numberofgoes))  
         (wotdidtheysay (make-em-guess "Pick a number between 1 and 100")	;Set'wotdidtheysay' by calling make-em-guess with the initial instruction.
    		    (make-em-guess (if (< wotdidtheysay secretnumber) "Too low, pick a higher number" "Too high, try something lower")))) ;Each loop 'wotdidtheysay' is reset by calling make-em-guess with appropriate instruction.
        ((= wotdidtheysay secretnumber);End test for 'do' loop.
         (format t "You won in ~a goes!" numberofgoes)))
    It was a much uglier beast using 'loop' before I saw a previous effort which showed that 'do' has all the control needed for this game, including getting all the variable scope right. Feel like I cheated after that, but once you see a nice idea you just can't get it out of your head...

  4. #34
    Join Date
    Nov 2009
    Location
    Ukraine
    Beans
    11

    Re: [Beginner] Programming Challenge: 6

    My first Lisp program ever:
    PHP Code:
    (setf *random-state* (make-random-state t))

    (
    format t "Guess the secret number (from 1 to 100):")
    (
    defvar secret_number (+ (random 1001))
    (
    loop for do
      (
    setq prediction (read))
      (if (> 
    prediction secret_number)
        (
    format t "Secret lower.~%"))
      (if (< 
    prediction secret_number)
        (
    format t "Secret higher.~%"))
      (if (= 
    prediction secret_number)
          (
    progn (format t "You win! Yes, it was ~D.~%It took you only ~D attempts!"
          
    secret_number i)
          (return 
    0)))


Page 4 of 4 FirstFirst ... 234

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •