Skip to content

Instantly share code, notes, and snippets.

@andy1138
Created September 20, 2012 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andy1138/3755454 to your computer and use it in GitHub Desktop.
Save andy1138/3755454 to your computer and use it in GitHub Desktop.
ProgFun Week1
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