Page 1 of 1

Help for the first program of a newbie

PostPosted: Sun Feb 28, 2016 7:45 am
by MrLouis
(I hope is the good section?!)
I begin the scheme and I try to do a small MasterMind. But I can't structure correctly the programm and I'm not sure to understand the indent style and how use and change the variables.
I don't understand too what is the different between 'and' and 'and?' :idea: ?
And why 'not' is not 'not?' ;) , they are twice predicates, no ?

Code: Select all
#lang racket
;; Master Mind ;;
(define nC 8)
(define lType [list nC nC nC nC])
(define nMyst (map (lambda (n) (random n)) lType))
(define (ask)
   (let* ([i (read [current-input-port])])
     (if (and (integer? i)
              (>= i 0)
              (< i nC)) i (begin [printf "You must write a number between 0 and ~a\n" (- nC 1)]
(define (L L1)
  (let* ([ans (list (ask) (ask) (ask) (ask))])
     (map [lambda (a b) (if (equal? a b) 'T [if [ormap equal? (list a a a a) L1] 'B 'F])] ans L1)
(do ([nT 0 (+ nT 1)]
     [iList (L nMyst) (append (L nMyst) iList)])
  ((equal? (second iList) nMyst) (printf "You win: ~a !!" nMyst))
    (printf "Round n°~a, \nF => False \nB => Bad Position \nT => True \nIt is incorrect: \n" nT)
    (for-each (lambda (arg) (printf "~a\n" arg)) iList)

Thanks for your future help !

Re: Help for the first program of a newbie

PostPosted: Thu Mar 03, 2016 10:48 am
by Goheeca
Here is a good article for the indentation of Common Lisp, that should be fine for scheme/racket too.
Changing variables is done through set! (the exclamation mark denotes that the function has side effects):
Code: Select all
(define x 0)
(set! x 1)

What concerns the pairs and/and? and not/not?, without question marks they are just building blocks of boolean expressions, whereas with question marks they combines predicate functions and returns a combined predicate function.