
-----------------------------------
wtd
Mon Jan 02, 2006 3:48 am

Test your skills (2006)
-----------------------------------
Io TYS.  :)

Io> foo(a, 42, a print)
42
==> 42
Io> foo(i, "foo", i with("!") print)
foo!
==> foo!

Write a method which makes the above possible.

Io> foo := method( 
       /* your code here */ 
    )

-----------------------------------
wtd
Sun Jan 15, 2006 5:09 pm


-----------------------------------
Write a function which appends lists.

(defun list-append (...) ...)

A few samples:

(list-append '(1 2 3) '(4 5 6))    ; (1 2 3 4 5 6)
(list-append '(1 2) '(3 4) '(5 6)) ; (1 2 3 4 5 6)

You may use the following functions or macros.


flet
cons
car
cdr
if
defun (but only the one, in the code I already demonstrated)
reduce

You may very explicitly not use the loop macro.

You may not have any code outside of the list-append function.

-----------------------------------
wtd
Fri Feb 03, 2006 2:27 am


-----------------------------------
C++ gurus: a TYS just for you.

Explain why this code will not compile.

#include 
#include 

int main()
{
   std::vector foo;

   return 0;
}


-----------------------------------
Martin
Tue Feb 07, 2006 3:18 am


-----------------------------------
C/C++ (or Java). Complete this function to determine if n is a power of 2.


int isPowerOfTwo (int n)
{
    return ;
}
bool isPowerOfTwo (int n)
{
    return ;
}
boolean isPowerOfTwo (int n)
{
    return ;
}

-----------------------------------
Andy
Tue Feb 07, 2006 9:37 am


-----------------------------------

bool isPowerOfTwo (int n)
{
    return n % 2 ? n == 1 : isPowerOfTwo(n/2);
}


does that count as cheating? lol

-----------------------------------
Martin
Tue Feb 07, 2006 7:23 pm


-----------------------------------
There's a much better way.

Think bitwise.

-----------------------------------
Andy
Tue Feb 07, 2006 10:27 pm


-----------------------------------
haha yea, i was going to do it bitwise, but then i got lazy =P, if i have time, i'll  do it at work tomorow

-----------------------------------
bugzpodder
Wed Feb 08, 2006 12:53 pm


-----------------------------------
bool isPowerOfTwo (int n)
{
    return n> is an operator in C++.  need a space in between.

-----------------------------------
wtd
Wed Feb 08, 2006 12:58 pm


-----------------------------------
#include 
#include 

int main()
{
   std::vector foo;

   return 0;
}


>> is an operator in C++.  need a space in between.

Yes, that is the solution.  But why is it a problem?  :)

-----------------------------------
bugzpodder
Wed Feb 08, 2006 1:07 pm


-----------------------------------
?  its a problem because >> is a predefined operator in C++.  But i thought I've already said that.

-----------------------------------
wtd
Wed Feb 08, 2006 1:08 pm


-----------------------------------
But surely C++ compilers can tell the difference?

-----------------------------------
bugzpodder
Wed Feb 08, 2006 2:33 pm


-----------------------------------
clearly not?  lol

-----------------------------------
Martin
Wed Feb 08, 2006 6:26 pm


-----------------------------------
bool isPowerOfTwo (int n)
{
    return n> some_other_value

Could clearly never be a type.  So... why does C++ get confused?  :)

-----------------------------------
Martin
Wed Feb 08, 2006 7:59 pm


-----------------------------------
Would this be a problem relating to operator overloading?

-----------------------------------
bugzpodder
Wed Feb 08, 2006 8:02 pm


-----------------------------------
bool isPowerOfTwo (int n)
{
    return n x * 3) (fun x -> x + 1)

And this is equivalent to:

let my_func x = x * 3 + 1

Write a function or function object in C++ which replicates the functionality of the compose function demonstrated above.

-----------------------------------
wtd
Wed Feb 15, 2006 11:08 pm


-----------------------------------
Give myList.ml as follows:

module type LIST_ITEM_TYPE =
  sig
    type t
  end
  
module MyList = 
  functor (Elt : LIST_ITEM_TYPE) ->
    struct
      type element = Elt.t
      
      type t = 
      | Empty
      | Node of element * t
      
      let empty = Empty
      
      let rec to_list =
        function 
        | Empty -> []
        | Node (v, rest) -> v :: to_list rest
        
      let rec from_list =
        function
        | [] -> Empty
        | x::xs -> Node (x, from_list xs)
        
      let rec fold_left f i =
        function
        | Empty -> i
        | Node (v, rest) -> fold_left f (f i v) rest
        
      let rec map f = 
        function
        | Empty -> Empty
        | Node (v, rest) -> Node (f v, map f rest)
        
      let iter f = fold_left (fun _ b -> f b) ()
      
      let length = fold_left (fun a _ -> a + 1) 0
      
      let rec append lst1 lst2 =
        match lst1, lst2 with
        | Empty, lst2 -> lst2
        | Node (v, rest), lst2 -> Node (v, append rest lst2)

      let reverse = fold_left (fun a b -> Node (b, a)) Empty
        
    end

Complete the following:

let (* your code *) in
   M.append (M.from_list [1; 2; 3]) (M.Node (4, M.Node (5, M.empty)))

-----------------------------------
rizzix
Thu Feb 16, 2006 7:20 pm


-----------------------------------
Write the following expression in point-free form:  f n = g (x n) (y n)

-----------------------------------
wtd
Wed Mar 01, 2006 9:53 pm


-----------------------------------
Explain why:

open Int64

let factorial n =
   let rec f n acc =
      if n = Int64.zero then
         acc
      else
         f (Int64.sub n Int64.one) (Int64.mul n acc)
   in
      f (Int64.of_int n) Int64.one

Does not cause a stack overflow when called with a value of ten million.

-----------------------------------
rizzix
Sat Mar 18, 2006 1:11 am


-----------------------------------
Write the following expression in point-free form:  f n = g (x n) (y n)

500 bits to the first person who correctly answers this. Oh and you need to explain your solution. :)

-----------------------------------
Martin
Tue Mar 21, 2006 6:22 pm


-----------------------------------
Rizzix - what is point free form?

-----------------------------------
rizzix
Wed Mar 22, 2006 1:58 am


-----------------------------------
eta reduce it to the point where there is no "n" (function parameter) visible in that code.

-----------------------------------
Martin
Wed Mar 22, 2006 2:17 am


-----------------------------------
Huh?

Is there a website on this, I'm not following...

-----------------------------------
rizzix
Wed Mar 22, 2006 5:07 pm


-----------------------------------
An example:f m n = g m (x n)f m n = ((g m) . x) nf m = (g m) . x

Eta-reduced, yet it is still not point free. As you can see there is still one parameter visible: m.

Another example:
f m n = n + mf m n = (+m) n
f m = (+) m
f = (+)

As you can see after completly eta-reducing it, there are no parameters visible. This final form is an example of point-free form.

-----------------------------------
tez_h
Mon Apr 03, 2006 9:13 pm


-----------------------------------
Write the following expression in point-free form:  f n = g (x n) (y n)

First, some useful functions:
(f . g) x  = f (g x)
flip f x y =  f y x
twice f x  = f x x


Now, we can derive a point-free expression of your function:
               f n = g (x n) (y n)
{defn of (.)}      = (g.x) n (y n)
{defn of flip}     = flip (g.x) (y n) n
{defn of (.)}      = (flip (g.x).y) n n
{defn of twice}    = twice (flip (g.x).y) n

                 f = twice (flip (g.x).y)


Is that the sort of answer you were looking for?

-Tez

-----------------------------------
rizzix
Tue Apr 04, 2006 12:56 am


-----------------------------------
Hey! Congrats and welcome to compsci! 

BTW: There is no need for you to redefine any of the Prelude functions (flip, composition). I was hoping you could do it without defining any of your own functions, like twice.

The truth is, that is not the answer I was hoping for, but it is an answer I was expecting -- or one kind of answer anyway. You get 350 bits for that one :)

Here's my solution that's synonymous to yours (i.e worth 350bits)

Helper function: toPair n = (n, n)Eta-reduction: f n = g (x n) (y n)            --          let a = g (x n)
    = (g (x n)) (y n)          -- therefore, f n = (a . y) n
    = ((g (x n)) . y) n        --                = ((. y) a) n
    = ((. y) (g (x n))) n      -- therefore, f n = ((. y) (g (x n))) 
    = ((. y) ((g . x) n)) n
    = ((. y) . g . x) n nTherefore,f = (uncurry $ (. y) . g . x) . toPair

Note that if we used your function twice, it was just a matter of:f = twice $ (. y) . g . x Eitherway, this is still not what I'm looking for, for that extra 150bits. :)

-----------------------------------
tez_h
Tue Apr 04, 2006 5:31 am


-----------------------------------
Hey! Congrats and welcome to compsci! 

BTW: There is no need for you to redefine any of the Prelude functions (flip, composition). I was hoping you could do it without defining any of your own functions, like twice.

Hi, and thanks.  Yeah, I thought I'd just write out the definitions for ease of reference.

The truth is, that is not the answer I was hoping for, but it is an answer I was expecting -- or one kind of answer anyway. You get 350 bits for that one :)

Eitherway, this is still not what I'm looking for, for that extra 150bits. :)
Well, I haven't quite worked out if you're expecting some ultra-clever combination of (.), ($), and flip, say, but here's one that only uses standard prelude functions, starting from near the end of my derivation:
f n = (flip (g.x).y) n n
    = foldr1 (flip (g.x).y) (replicate 2 n)
    = (foldr1 (flip (g.x).y) . replicate 2) n
  f = foldr1 (flip (g.x).y) . replicate 2
Not pretty, and certainly point-less  :wink: .  Similarly, starting from near the end of your derivation:
f n = ((. y) . g . x) n n
    = foldr1 ((. y) . g . x) (replicate 2 n)
    = (foldr1 ((. y) . g . x) . replicate 2) n
  f = foldr1 ((. y) . g . x) . replicate 2
Is that more what you're looking for?

-Tez

-----------------------------------
rizzix
Tue Apr 04, 2006 11:51 am


-----------------------------------
No. Let me give you a hint: I'm looking for a simpler solution ;)
Another hint: (->a)

-----------------------------------
tez_h
Wed Apr 05, 2006 4:27 am

(-&gt; a) monad
-----------------------------------
No. Let me give you a hint: I'm looking for a simpler solution ;)
Another hint: (->a)

Yes, well I did see somewhere that there is a "(-> a)" monad that lambdabot (on #haskell on freenode) uses to transform point-wise expressions into point-free form.  But since I haven't really done much proper haskell programming for a good few years, and googling for ' "(-> a)" monad ' turned up very little, I'm afraid I'm going to have to admit defeat here.

I'd love it if you could provide a reference or something about the (-> a) monad, or even if it has a more googlable name.  Or PM me a more obvious hint!

Thanks for your time and the challenge.  Now I need something to get me to learn about arrows!

-Tez

-----------------------------------
rizzix
Thu Apr 06, 2006 1:04 am


-----------------------------------
Just lookup the Reader Monad. (->a) is just an instance of Control.Monad.Reader. Where (->) is the function type constructor.

http://www.nomaware.com/monads/html/readermonad.html

-----------------------------------
wtd
Thu Apr 20, 2006 2:34 pm


-----------------------------------
C++ today.

#include 
#include 
#include 

using namespace std;

char upcase(const char original);
string capitalize(const string& s);

int main(int argc, char **argv)
{
   string *args = new string[argc - 1];
   copy(argv + 1, argv + argc, args);
   transform(args, args + argc - 1, args, capitalize);

   for (int i = 0; i < argc - 1; i++)
   {
      cout 