This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define divisible? | |
(lambda (a n) | |
(eq? (remainder a n) 0) | |
)) | |
(define fizz-buzz | |
(lambda (i) | |
(cond | |
((eq? i 0) '0) | |
(else (cond |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package exercises | |
object NinetyNine { | |
//P01 | |
def last[A](xs: List[A]): A = xs match { | |
case x :: Nil => x | |
case x :: xs => last(xs) | |
case Nil => throw new NoSuchElementException | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* no resuelve lo que se busca, da #t si coincide algun elemento en la misma posicion en ambas listas *) | |
(define member-of-2? | |
(lambda (a lst1 lst2) | |
(cond | |
((or (null? lst1) (null? lst2) ) #f) | |
((and (eq? a (car lst1)) (eq? a (car lst2)) ) #t) | |
( else (member-of-2? a (cdr lst1) (cdr lst2) )) | |
))) | |
(* usando member? *) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val Π = Math.Pi | |
def √(x:Double)=Math.sqrt(x) | |
val x= √(9*Π) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LogicBoolean(val value:Boolean) { | |
import LogicBoolean.¬ | |
override def toString=value.toString | |
def V(b:LogicBoolean)=new LogicBoolean(this.value || b.value) | |
def Λ(b:LogicBoolean)=new LogicBoolean(this.value && b.value) | |
def →(b:LogicBoolean)=(¬(this)) V (this Λ b) | |
} | |
object LogicBoolean { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ∑(r:Range)(f:Int =>Int)=r.reduceLeft(_+ f(_)) | |
def ∏(r:Range)(f:Int =>Int)=r.reduceLeft(_* f(_)) | |
// And now we can write: | |
val s= ∑(1 to 100)(x=>x^2) | |
val p= ∑(1 to 100 by 2)(x=>x^2) | |
val y= ∏(1 to 30 by 3)(_) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo | |
object UnifiedTypes { | |
def main(args: Array[String]) { | |
val set = new scala.collection.mutable.HashSet[Any] | |
set += "This is a string" // add a string | |
set += 732 // add a number | |
set += 'c' // add a character | |
set += true // add a boolean value | |
set += main _ // add the main function |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo | |
//mejor en la REPL | |
class FP { | |
/* | |
new Function1[Int, Int] { | |
def apply(x: Int): Int = x + 1 | |
} | |
*/ | |
(x: Int) => x + 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( define reverse1 | |
( lambda (l) | |
(define reverse-rec | |
(lambda (acc-l l) | |
(cond | |
( (eq? l '()) acc-l ) | |
( else ( reverse-rec (cons (car l) acc-l) (cdr l))) | |
))) | |
(reverse-rec '() l))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
subseq :: Int ->[a]->[[a]] | |
subseq _ [] = [] | |
subseq n xs | len_xs<n = [] | |
| len_xs==n = [xs] | |
| otherwise = take n xs : subseq n (tail xs) | |
where len_xs = length xs | |
subseq' :: Int ->[a]->[[a]] | |
subseq' n xs = filter (flip isInfixOf xs) (filter ( eq_len ) (subsequences xs) ) where eq_len ys = length ys == n |