创建k大小排列而不定义更多函数

时间:2017-07-09 11:02:42

标签: scheme racket

是否可以实现获得列表和k的Scheme函数(一个函数 - 它的重要),并且检索k的大小的排列,例如:(1 2 3),k = 2将输出{(1 ,1),(1,2),(1,3),(2,1),(2,2),.....}(9选项)。?

2 个答案:

答案 0 :(得分:-1)

只要你有lambda:

,就可以做任何事情而无需定义任何东西
(define (fib n)
  ;; bad internal definition
  (define (helper n a b)
    (if (zero? n)
        a
        (helper (- n 1) b (+ a b))))
  (helper n 0 1))

使用Z组合器:

(define Z
  (lambda (f)
    ((lambda (g)
       (f (lambda args (apply (g g) args))))
     (lambda (g)
       (f (lambda args (apply (g g) args)))))))

(define (fib n)
  ((Z (lambda (helper)
        (lambda (n a b)
          (if (zero? n)
              a
              (helper (- n 1) b (+ a b))))))
   n 0 1))

现在我们永远不会调用Z,所以我们可以在函数中用Z替换Z的值,它会做同样的事情:

(define (fib n)
  (((lambda (f)
      ((lambda (g)
         (f (lambda args (apply (g g) args))))
       (lambda (g)
         (f (lambda args (apply (g g) args))))))
    (lambda (helper)
      (lambda (n a b)
        (if (zero? n)
            a
            (helper (- n 1) b (+ a b))))))
   n 0 1))

你去了,Alonzo Church保存。

答案 1 :(得分:-1)

这不仅可行,而且很容易。只需使用public static void main(String[] args) throws InterruptedException { // init shell ProcessBuilder builder = new ProcessBuilder("cmd"); Scanner scanner = null; BufferedWriter writer = null; try { scanner = new Scanner (System.in); System.out.print("Enter your MS ID : "); String user = scanner.nextLine(); System.out.print("Enter your MS Password : "); String pass = scanner.nextLine(); Process p = builder.start(); writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream())); writer.write("oc logout"); writer.newLine(); writer.flush(); writer.write("oc login <private-url>"); writer.newLine(); writer.flush(); writer.write(user); writer.newLine(); writer.flush(); writer.write(pass); writer.newLine(); writer.flush(); //Writing this will end the process after login is done // writer.write("exit"); // writer.newLine(); // writer.flush(); Thread t = new Thread(new Runnable() { public void run() { try(BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))) { ; String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (java.io.IOException e) { e.printStackTrace(); } } }); t.setDaemon(true); t.start(); p.waitFor(); } catch (IOException e) { System.out.println(e); } finally { scanner.close(); } }

loop