Clojure - funció until W94600


Statement
 

pdf   zip

thehtml

En Haskell tenim una funció until que, donat un predicat p, una funció f i un valor inicial v, va aplicant la funció f tal que v, f(v), f(f(v))… fins que es satisfa el predicat. Per exemple:

(until #(> % 100) #(* 2 %) 1)


té com a resultat 128.

  1. Implementeu la funció until1 en Clojure utilitzant el recur.
  2. Implementeu la funció until2 en Clojure amb funcions d’ordre superior.
  3. Implementeu l’algorisme d’Euclides en forma de funció mcd per calcular el màxim comú divisor utilitzant una de les funcions until.
    Entrada: a, b 1. Si a=b, mcd=a, fi. 2. Si a>b, canvi de a per a-b, anar a 1. 3. Si a<b, canvi de b per b-a, anar a 1.
Public test cases
  • Input

    (until1 #(> % 100) #(* 2 %) 1)
    

    Output

    128
    
  • Input

    (until2 #(> % 100) #(* 2 %) 1)
    

    Output

    128
    
  • Input

    (mcd 14 8)
    

    Output

    2
    
  • Information
    Author
    Jordi Delgado / Gerard Escudero
    Language
    Catalan
    Official solutions
    Clojure
    User solutions
    Clojure