Author |
Message |
Martin
|
Posted: Wed Feb 08, 2006 7:59 pm Post subject: (No subject) |
|
|
Would this be a problem relating to operator overloading? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
bugzpodder
|
Posted: Wed Feb 08, 2006 8:02 pm Post subject: (No subject) |
|
|
bugzpodder wrote: c++: | bool isPowerOfTwo (int n)
{
return n<0?0:(n^(n-1))==-1;
} |
actually this is wrong. it should be
c++: | bool isPowerOfTwo (int n)
{
return n<0?0:(n^(n-1))==2*(n-1)+1;
} |
|
|
|
|
|
|
wtd
|
Posted: Wed Feb 08, 2006 8:57 pm Post subject: (No subject) |
|
|
Martin wrote: Would this be a problem relating to operator overloading?
More fundamental. |
|
|
|
|
|
Martin
|
Posted: Wed Feb 08, 2006 9:44 pm Post subject: (No subject) |
|
|
I'm at a loss.
Can we have a hint? |
|
|
|
|
|
wtd
|
Posted: Wed Feb 08, 2006 9:51 pm Post subject: (No subject) |
|
|
C++ templates work with more than just types. They permit the use of values as template parameters as well.
code: | some_value >> some_other_value |
That isn't a type, but it is a value. The compiler is stupid enough that it sees something like "foo>>" as potentially being a value that could be a template parameter.
The problem exists because the parser is not aware enough of the semantics of the language it's parsing.
One of the requirements of C++0x compliant compilers will be that they not exhibit this problem. |
|
|
|
|
|
wtd
|
Posted: Fri Feb 10, 2006 5:29 pm Post subject: (No subject) |
|
|
Another C++ question
In Objective-Caml it's relatively easy to define a function which can compose two other functions to create a new function.
code: | let compose f g x = g (f x) |
Now I can write:
code: | let my_func = compose (fun x -> x * 3) (fun x -> x + 1) |
And this is equivalent to:
code: | 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
|
Posted: Wed Feb 15, 2006 11:08 pm Post subject: (No subject) |
|
|
Give myList.ml as follows:
code: | 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:
code: | let (* your code *) in
M.append (M.from_list [1; 2; 3]) (M.Node (4, M.Node (5, M.empty))) |
|
|
|
|
|
|
rizzix
|
Posted: Thu Feb 16, 2006 7:20 pm Post subject: (No subject) |
|
|
Write the following expression in point-free form: Haskell: | f n = g (x n) (y n) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Wed Mar 01, 2006 9:53 pm Post subject: (No subject) |
|
|
Explain why:
code: | 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
|
Posted: Sat Mar 18, 2006 1:11 am Post subject: (No subject) |
|
|
rizzix wrote: Write the following expression in point-free form: Haskell: | 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
|
Posted: Tue Mar 21, 2006 6:22 pm Post subject: (No subject) |
|
|
Rizzix - what is point free form? |
|
|
|
|
|
rizzix
|
Posted: Wed Mar 22, 2006 1:58 am Post subject: (No subject) |
|
|
eta reduce it to the point where there is no "n" (function parameter) visible in that code. |
|
|
|
|
|
Martin
|
Posted: Wed Mar 22, 2006 2:17 am Post subject: (No subject) |
|
|
Huh?
Is there a website on this, I'm not following... |
|
|
|
|
|
rizzix
|
Posted: Wed Mar 22, 2006 5:07 pm Post subject: (No subject) |
|
|
An example: Haskell: | f m n = g m (x n) |
Haskell: | f m n = ((g m) . x) n |
Eta-reduced, yet it is still not point free. As you can see there is still one parameter visible: m.
Another example:
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
|
Posted: Mon Apr 03, 2006 9:13 pm Post subject: (No subject) |
|
|
rizzix wrote: Write the following expression in point-free form: Haskell: | f n = g (x n) (y n) |
First, some useful functions:
Definitions: | (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:
Solution: | 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 |
|
|
|
|
|
|