Created
September 20, 2012 11:46
-
-
Save andy1138/3755454 to your computer and use it in GitHub Desktop.
ProgFun Week1
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
Call-by-name vs call-by-value | |
1) Write a timer function, so this will work | |
scala> def sleep( n:Int) { Thread.sleep(n) } | |
scala> timer( sleep(5000) ) | |
scala> Time 5 secs | |
2) Given | |
scala> def dTrue = { Thread.sleep(5000); true } | |
scala> def dFalse = { Thread.sleep(3000); false } | |
using only || and && create a method that sleeps for 11 seconds | |
Recursion | |
1) Write a recursive function that computes the sum of all numbers from 1 to n, where n is given as parameter. | |
//return the sum 1+ 2+ 3+ ...+ n | |
def sum( n:Int):Int | |
2) Write a recursive function that finds and returns the minimum element in an array, where the array is parameters. | |
//return the minimum element in a[] | |
def findmin(a: Array[Int]): Int | |
3) Write a recursive function that computes and returns the sum of all elements in an array. | |
//return the sum of all elements in a[] | |
def findsum(a:Array[Int] ):Int | |
4) Write a recursive function that determines whether an array is a palindrome, where the array and its size are given as parameters. | |
//returns 1 if a[] is a palindrome, 0 otherwise | |
int ispalindrome(char a[], int n) | |
5) add @tailrec | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment