Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Convert this Ada block to Go, preserving its control flow and logic.
pragma Initialize_Scalars; with Ada.Text_IO; use Ada.Text_IO; procedure Invalid_Value is type Color is (Red, Green, Blue); X : Float; Y : Color; begin if not X'Valid then Put_Line ("X is not valid"); end if; X := 1.0; if X'Valid then Put_Line ("X is" & Float'Image (X)); end if; if not Y'Valid then Put_Line ("Y is not valid"); end if; Y := Green; if Y'Valid then Put_Line ("Y is " & Color'Image (Y)); end if; end Invalid_Value;
package main import "fmt" var ( s []int p *int f func() i interface{} m map[int]int c chan int ) func main() { fmt.Println("Exercise nil objects:") status() s = make([]int, 1) p = &s[0] f = func() { fmt.Println("function call") } i = user(0) m = make(map[int]int) c = make(chan int, 1) fmt.Println("\nExercise objects after initialization:") status() } type user int func (user) m() { fmt.Println("method call") } func status() { trySlice() tryPointer() tryFunction() tryInterface() tryMap() tryChannel() } func reportPanic() { if x := recover(); x != nil { fmt.Println("panic:", x) } } func trySlice() { defer reportPanic() fmt.Println("s[0] =", s[0]) } func tryPointer() { defer reportPanic() fmt.Println("*p =", *p) } func tryFunction() { defer reportPanic() f() } func tryInterface() { defer reportPanic() switch i.(type) { case nil: fmt.Println("i is nil interface") case interface { m() }: fmt.Println("i has method m") } i.(interface { m() }).m() } func tryMap() { defer reportPanic() m[0] = 0 fmt.Println("m[0] =", m[0]) } func tryChannel() { defer reportPanic() close(c) fmt.Println("channel closed") }
Produce a language-to-language conversion: from Ada to Java, same semantics.
pragma Initialize_Scalars; with Ada.Text_IO; use Ada.Text_IO; procedure Invalid_Value is type Color is (Red, Green, Blue); X : Float; Y : Color; begin if not X'Valid then Put_Line ("X is not valid"); end if; X := 1.0; if X'Valid then Put_Line ("X is" & Float'Image (X)); end if; if not Y'Valid then Put_Line ("Y is not valid"); end if; Y := Green; if Y'Valid then Put_Line ("Y is " & Color'Image (Y)); end if; end Invalid_Value;
String string = null; System.out.println(string); System.out.println(string.length());
Write a version of this Ada function in Python with identical behavior.
pragma Initialize_Scalars; with Ada.Text_IO; use Ada.Text_IO; procedure Invalid_Value is type Color is (Red, Green, Blue); X : Float; Y : Color; begin if not X'Valid then Put_Line ("X is not valid"); end if; X := 1.0; if X'Valid then Put_Line ("X is" & Float'Image (X)); end if; if not Y'Valid then Put_Line ("Y is not valid"); end if; Y := Green; if Y'Valid then Put_Line ("Y is " & Color'Image (Y)); end if; end Invalid_Value;
try: name except NameError: print "name is undefined at first check" name = "Chocolate" try: name except NameError: print "name is undefined at second check" del name try: name except NameError: print "name is undefined at third check" name = 42 try: name except NameError: print "name is undefined at fourth check" print "Done"
Change the following Arturo code into C without altering its purpose.
undef: null print undef
#include <stdio.h> #include <stdlib.h> int main() { int junk, *junkp; printf("junk: %d\n", junk); junkp = malloc(sizeof *junkp); if (junkp) printf("*junkp: %d\n", *junkp); return 0; }
Rewrite this program in C++ while keeping its functionality equivalent to the Arturo version.
undef: null print undef
#include <iostream> int main() { int undefined; if (undefined == 42) { std::cout << "42"; } if (undefined != 42) { std::cout << "not 42"; } }
Convert the following code from Arturo to Python, ensuring the logic remains intact.
undef: null print undef
try: name except NameError: print "name is undefined at first check" name = "Chocolate" try: name except NameError: print "name is undefined at second check" del name try: name except NameError: print "name is undefined at third check" name = 42 try: name except NameError: print "name is undefined at fourth check" print "Done"
Produce a functionally identical Go code for the snippet given in Arturo.
undef: null print undef
package main import "fmt" var ( s []int p *int f func() i interface{} m map[int]int c chan int ) func main() { fmt.Println("Exercise nil objects:") status() s = make([]int, 1) p = &s[0] f = func() { fmt.Println("function call") } i = user(0) m = make(map[int]int) c = make(chan int, 1) fmt.Println("\nExercise objects after initialization:") status() } type user int func (user) m() { fmt.Println("method call") } func status() { trySlice() tryPointer() tryFunction() tryInterface() tryMap() tryChannel() } func reportPanic() { if x := recover(); x != nil { fmt.Println("panic:", x) } } func trySlice() { defer reportPanic() fmt.Println("s[0] =", s[0]) } func tryPointer() { defer reportPanic() fmt.Println("*p =", *p) } func tryFunction() { defer reportPanic() f() } func tryInterface() { defer reportPanic() switch i.(type) { case nil: fmt.Println("i is nil interface") case interface { m() }: fmt.Println("i has method m") } i.(interface { m() }).m() } func tryMap() { defer reportPanic() m[0] = 0 fmt.Println("m[0] =", m[0]) } func tryChannel() { defer reportPanic() close(c) fmt.Println("channel closed") }
Produce a functionally identical C code for the snippet given in BBC_Basic.
ok% = TRUE ON ERROR LOCAL IF ERR<>26 REPORT : END ELSE ok% = FALSE IF ok% THEN PRINT variable$ ELSE PRINT "Not defined" ENDIF RESTORE ERROR
#include <stdio.h> #include <stdlib.h> int main() { int junk, *junkp; printf("junk: %d\n", junk); junkp = malloc(sizeof *junkp); if (junkp) printf("*junkp: %d\n", *junkp); return 0; }
Generate a C# translation of this BBC_Basic snippet without changing its computational steps.
ok% = TRUE ON ERROR LOCAL IF ERR<>26 REPORT : END ELSE ok% = FALSE IF ok% THEN PRINT variable$ ELSE PRINT "Not defined" ENDIF RESTORE ERROR
string foo = null;
Port the following code from BBC_Basic to C++ with equivalent syntax and logic.
ok% = TRUE ON ERROR LOCAL IF ERR<>26 REPORT : END ELSE ok% = FALSE IF ok% THEN PRINT variable$ ELSE PRINT "Not defined" ENDIF RESTORE ERROR
#include <iostream> int main() { int undefined; if (undefined == 42) { std::cout << "42"; } if (undefined != 42) { std::cout << "not 42"; } }
Convert this BBC_Basic snippet to Java and keep its semantics consistent.
ok% = TRUE ON ERROR LOCAL IF ERR<>26 REPORT : END ELSE ok% = FALSE IF ok% THEN PRINT variable$ ELSE PRINT "Not defined" ENDIF RESTORE ERROR
String string = null; System.out.println(string); System.out.println(string.length());
Translate this program into Python but keep the logic exactly as in BBC_Basic.
ok% = TRUE ON ERROR LOCAL IF ERR<>26 REPORT : END ELSE ok% = FALSE IF ok% THEN PRINT variable$ ELSE PRINT "Not defined" ENDIF RESTORE ERROR
try: name except NameError: print "name is undefined at first check" name = "Chocolate" try: name except NameError: print "name is undefined at second check" del name try: name except NameError: print "name is undefined at third check" name = 42 try: name except NameError: print "name is undefined at fourth check" print "Done"
Convert this BBC_Basic block to Go, preserving its control flow and logic.
ok% = TRUE ON ERROR LOCAL IF ERR<>26 REPORT : END ELSE ok% = FALSE IF ok% THEN PRINT variable$ ELSE PRINT "Not defined" ENDIF RESTORE ERROR
package main import "fmt" var ( s []int p *int f func() i interface{} m map[int]int c chan int ) func main() { fmt.Println("Exercise nil objects:") status() s = make([]int, 1) p = &s[0] f = func() { fmt.Println("function call") } i = user(0) m = make(map[int]int) c = make(chan int, 1) fmt.Println("\nExercise objects after initialization:") status() } type user int func (user) m() { fmt.Println("method call") } func status() { trySlice() tryPointer() tryFunction() tryInterface() tryMap() tryChannel() } func reportPanic() { if x := recover(); x != nil { fmt.Println("panic:", x) } } func trySlice() { defer reportPanic() fmt.Println("s[0] =", s[0]) } func tryPointer() { defer reportPanic() fmt.Println("*p =", *p) } func tryFunction() { defer reportPanic() f() } func tryInterface() { defer reportPanic() switch i.(type) { case nil: fmt.Println("i is nil interface") case interface { m() }: fmt.Println("i has method m") } i.(interface { m() }).m() } func tryMap() { defer reportPanic() m[0] = 0 fmt.Println("m[0] =", m[0]) } func tryChannel() { defer reportPanic() close(c) fmt.Println("channel closed") }
Can you help me rewrite this code in C instead of Common_Lisp, keeping it the same logically?
(defvar *x*) (defvar *y* 42) (special-variable-p '*x*) -> T (boundp '*x*) -> NIL (boundp '*y*) -> T (special-variable-p '*z*) -> NIL
#include <stdio.h> #include <stdlib.h> int main() { int junk, *junkp; printf("junk: %d\n", junk); junkp = malloc(sizeof *junkp); if (junkp) printf("*junkp: %d\n", *junkp); return 0; }
Translate the given Common_Lisp code snippet into C# without altering its behavior.
(defvar *x*) (defvar *y* 42) (special-variable-p '*x*) -> T (boundp '*x*) -> NIL (boundp '*y*) -> T (special-variable-p '*z*) -> NIL
string foo = null;
Keep all operations the same but rewrite the snippet in C++.
(defvar *x*) (defvar *y* 42) (special-variable-p '*x*) -> T (boundp '*x*) -> NIL (boundp '*y*) -> T (special-variable-p '*z*) -> NIL
#include <iostream> int main() { int undefined; if (undefined == 42) { std::cout << "42"; } if (undefined != 42) { std::cout << "not 42"; } }
Rewrite the snippet below in Java so it works the same as the original Common_Lisp code.
(defvar *x*) (defvar *y* 42) (special-variable-p '*x*) -> T (boundp '*x*) -> NIL (boundp '*y*) -> T (special-variable-p '*z*) -> NIL
String string = null; System.out.println(string); System.out.println(string.length());
Produce a language-to-language conversion: from Common_Lisp to Python, same semantics.
(defvar *x*) (defvar *y* 42) (special-variable-p '*x*) -> T (boundp '*x*) -> NIL (boundp '*y*) -> T (special-variable-p '*z*) -> NIL
try: name except NameError: print "name is undefined at first check" name = "Chocolate" try: name except NameError: print "name is undefined at second check" del name try: name except NameError: print "name is undefined at third check" name = 42 try: name except NameError: print "name is undefined at fourth check" print "Done"
Write the same algorithm in Go as shown in this Common_Lisp implementation.
(defvar *x*) (defvar *y* 42) (special-variable-p '*x*) -> T (boundp '*x*) -> NIL (boundp '*y*) -> T (special-variable-p '*z*) -> NIL
package main import "fmt" var ( s []int p *int f func() i interface{} m map[int]int c chan int ) func main() { fmt.Println("Exercise nil objects:") status() s = make([]int, 1) p = &s[0] f = func() { fmt.Println("function call") } i = user(0) m = make(map[int]int) c = make(chan int, 1) fmt.Println("\nExercise objects after initialization:") status() } type user int func (user) m() { fmt.Println("method call") } func status() { trySlice() tryPointer() tryFunction() tryInterface() tryMap() tryChannel() } func reportPanic() { if x := recover(); x != nil { fmt.Println("panic:", x) } } func trySlice() { defer reportPanic() fmt.Println("s[0] =", s[0]) } func tryPointer() { defer reportPanic() fmt.Println("*p =", *p) } func tryFunction() { defer reportPanic() f() } func tryInterface() { defer reportPanic() switch i.(type) { case nil: fmt.Println("i is nil interface") case interface { m() }: fmt.Println("i has method m") } i.(interface { m() }).m() } func tryMap() { defer reportPanic() m[0] = 0 fmt.Println("m[0] =", m[0]) } func tryChannel() { defer reportPanic() close(c) fmt.Println("channel closed") }
Ensure the translated C code behaves exactly like the original D snippet.
void main() { int a = 5; double b = 5.0; char c = 'f'; int[] d = [1, 2, 3]; int aa; double bb; char cc; int[] dd; int[3] ee; int aaa = void; double[] bbb = void; int[3] eee = void; }
#include <stdio.h> #include <stdlib.h> int main() { int junk, *junkp; printf("junk: %d\n", junk); junkp = malloc(sizeof *junkp); if (junkp) printf("*junkp: %d\n", *junkp); return 0; }
Keep all operations the same but rewrite the snippet in C#.
void main() { int a = 5; double b = 5.0; char c = 'f'; int[] d = [1, 2, 3]; int aa; double bb; char cc; int[] dd; int[3] ee; int aaa = void; double[] bbb = void; int[3] eee = void; }
string foo = null;
Keep all operations the same but rewrite the snippet in C++.
void main() { int a = 5; double b = 5.0; char c = 'f'; int[] d = [1, 2, 3]; int aa; double bb; char cc; int[] dd; int[3] ee; int aaa = void; double[] bbb = void; int[3] eee = void; }
#include <iostream> int main() { int undefined; if (undefined == 42) { std::cout << "42"; } if (undefined != 42) { std::cout << "not 42"; } }
Convert this D snippet to Java and keep its semantics consistent.
void main() { int a = 5; double b = 5.0; char c = 'f'; int[] d = [1, 2, 3]; int aa; double bb; char cc; int[] dd; int[3] ee; int aaa = void; double[] bbb = void; int[3] eee = void; }
String string = null; System.out.println(string); System.out.println(string.length());
Translate the given D code snippet into Python without altering its behavior.
void main() { int a = 5; double b = 5.0; char c = 'f'; int[] d = [1, 2, 3]; int aa; double bb; char cc; int[] dd; int[3] ee; int aaa = void; double[] bbb = void; int[3] eee = void; }
try: name except NameError: print "name is undefined at first check" name = "Chocolate" try: name except NameError: print "name is undefined at second check" del name try: name except NameError: print "name is undefined at third check" name = 42 try: name except NameError: print "name is undefined at fourth check" print "Done"
Preserve the algorithm and functionality while converting the code from D to Go.
void main() { int a = 5; double b = 5.0; char c = 'f'; int[] d = [1, 2, 3]; int aa; double bb; char cc; int[] dd; int[3] ee; int aaa = void; double[] bbb = void; int[3] eee = void; }
package main import "fmt" var ( s []int p *int f func() i interface{} m map[int]int c chan int ) func main() { fmt.Println("Exercise nil objects:") status() s = make([]int, 1) p = &s[0] f = func() { fmt.Println("function call") } i = user(0) m = make(map[int]int) c = make(chan int, 1) fmt.Println("\nExercise objects after initialization:") status() } type user int func (user) m() { fmt.Println("method call") } func status() { trySlice() tryPointer() tryFunction() tryInterface() tryMap() tryChannel() } func reportPanic() { if x := recover(); x != nil { fmt.Println("panic:", x) } } func trySlice() { defer reportPanic() fmt.Println("s[0] =", s[0]) } func tryPointer() { defer reportPanic() fmt.Println("*p =", *p) } func tryFunction() { defer reportPanic() f() } func tryInterface() { defer reportPanic() switch i.(type) { case nil: fmt.Println("i is nil interface") case interface { m() }: fmt.Println("i has method m") } i.(interface { m() }).m() } func tryMap() { defer reportPanic() m[0] = 0 fmt.Println("m[0] =", m[0]) } func tryChannel() { defer reportPanic() close(c) fmt.Println("channel closed") }
Change the following Delphi code into C without altering its purpose.
var P: PInteger; begin New(P); try If Assigned(P) Then begin P^ := 42; end; finally Dispose(P); end; end;
#include <stdio.h> #include <stdlib.h> int main() { int junk, *junkp; printf("junk: %d\n", junk); junkp = malloc(sizeof *junkp); if (junkp) printf("*junkp: %d\n", *junkp); return 0; }
Generate a C# translation of this Delphi snippet without changing its computational steps.
var P: PInteger; begin New(P); try If Assigned(P) Then begin P^ := 42; end; finally Dispose(P); end; end;
string foo = null;
Write the same code in C++ as shown below in Delphi.
var P: PInteger; begin New(P); try If Assigned(P) Then begin P^ := 42; end; finally Dispose(P); end; end;
#include <iostream> int main() { int undefined; if (undefined == 42) { std::cout << "42"; } if (undefined != 42) { std::cout << "not 42"; } }
Port the following code from Delphi to Java with equivalent syntax and logic.
var P: PInteger; begin New(P); try If Assigned(P) Then begin P^ := 42; end; finally Dispose(P); end; end;
String string = null; System.out.println(string); System.out.println(string.length());
Produce a functionally identical Python code for the snippet given in Delphi.
var P: PInteger; begin New(P); try If Assigned(P) Then begin P^ := 42; end; finally Dispose(P); end; end;
try: name except NameError: print "name is undefined at first check" name = "Chocolate" try: name except NameError: print "name is undefined at second check" del name try: name except NameError: print "name is undefined at third check" name = 42 try: name except NameError: print "name is undefined at fourth check" print "Done"
Can you help me rewrite this code in Go instead of Delphi, keeping it the same logically?
var P: PInteger; begin New(P); try If Assigned(P) Then begin P^ := 42; end; finally Dispose(P); end; end;
package main import "fmt" var ( s []int p *int f func() i interface{} m map[int]int c chan int ) func main() { fmt.Println("Exercise nil objects:") status() s = make([]int, 1) p = &s[0] f = func() { fmt.Println("function call") } i = user(0) m = make(map[int]int) c = make(chan int, 1) fmt.Println("\nExercise objects after initialization:") status() } type user int func (user) m() { fmt.Println("method call") } func status() { trySlice() tryPointer() tryFunction() tryInterface() tryMap() tryChannel() } func reportPanic() { if x := recover(); x != nil { fmt.Println("panic:", x) } } func trySlice() { defer reportPanic() fmt.Println("s[0] =", s[0]) } func tryPointer() { defer reportPanic() fmt.Println("*p =", *p) } func tryFunction() { defer reportPanic() f() } func tryInterface() { defer reportPanic() switch i.(type) { case nil: fmt.Println("i is nil interface") case interface { m() }: fmt.Println("i has method m") } i.(interface { m() }).m() } func tryMap() { defer reportPanic() m[0] = 0 fmt.Println("m[0] =", m[0]) } func tryChannel() { defer reportPanic() close(c) fmt.Println("channel closed") }
Maintain the same structure and functionality when rewriting this code in C.
-module( undefined_values ). -export( [task/0] ). -record( a_record, {member_1, member_2} ). task() -> Record = #a_record{member_1=a_value}, io:fwrite( "Record member_1 ~p, member_2 ~p~n", [Record#a_record.member_1, Record#a_record.member_2] ), io:fwrite( "Member_2 is undefined ~p~n", [Record#a_record.member_2 =:= undefined] ).
#include <stdio.h> #include <stdlib.h> int main() { int junk, *junkp; printf("junk: %d\n", junk); junkp = malloc(sizeof *junkp); if (junkp) printf("*junkp: %d\n", *junkp); return 0; }
Change the programming language of this snippet from Erlang to C# without modifying what it does.
-module( undefined_values ). -export( [task/0] ). -record( a_record, {member_1, member_2} ). task() -> Record = #a_record{member_1=a_value}, io:fwrite( "Record member_1 ~p, member_2 ~p~n", [Record#a_record.member_1, Record#a_record.member_2] ), io:fwrite( "Member_2 is undefined ~p~n", [Record#a_record.member_2 =:= undefined] ).
string foo = null;
Produce a language-to-language conversion: from Erlang to C++, same semantics.
-module( undefined_values ). -export( [task/0] ). -record( a_record, {member_1, member_2} ). task() -> Record = #a_record{member_1=a_value}, io:fwrite( "Record member_1 ~p, member_2 ~p~n", [Record#a_record.member_1, Record#a_record.member_2] ), io:fwrite( "Member_2 is undefined ~p~n", [Record#a_record.member_2 =:= undefined] ).
#include <iostream> int main() { int undefined; if (undefined == 42) { std::cout << "42"; } if (undefined != 42) { std::cout << "not 42"; } }
Write the same algorithm in Python as shown in this Erlang implementation.
-module( undefined_values ). -export( [task/0] ). -record( a_record, {member_1, member_2} ). task() -> Record = #a_record{member_1=a_value}, io:fwrite( "Record member_1 ~p, member_2 ~p~n", [Record#a_record.member_1, Record#a_record.member_2] ), io:fwrite( "Member_2 is undefined ~p~n", [Record#a_record.member_2 =:= undefined] ).
try: name except NameError: print "name is undefined at first check" name = "Chocolate" try: name except NameError: print "name is undefined at second check" del name try: name except NameError: print "name is undefined at third check" name = 42 try: name except NameError: print "name is undefined at fourth check" print "Done"
Port the following code from Erlang to Go with equivalent syntax and logic.
-module( undefined_values ). -export( [task/0] ). -record( a_record, {member_1, member_2} ). task() -> Record = #a_record{member_1=a_value}, io:fwrite( "Record member_1 ~p, member_2 ~p~n", [Record#a_record.member_1, Record#a_record.member_2] ), io:fwrite( "Member_2 is undefined ~p~n", [Record#a_record.member_2 =:= undefined] ).
package main import "fmt" var ( s []int p *int f func() i interface{} m map[int]int c chan int ) func main() { fmt.Println("Exercise nil objects:") status() s = make([]int, 1) p = &s[0] f = func() { fmt.Println("function call") } i = user(0) m = make(map[int]int) c = make(chan int, 1) fmt.Println("\nExercise objects after initialization:") status() } type user int func (user) m() { fmt.Println("method call") } func status() { trySlice() tryPointer() tryFunction() tryInterface() tryMap() tryChannel() } func reportPanic() { if x := recover(); x != nil { fmt.Println("panic:", x) } } func trySlice() { defer reportPanic() fmt.Println("s[0] =", s[0]) } func tryPointer() { defer reportPanic() fmt.Println("*p =", *p) } func tryFunction() { defer reportPanic() f() } func tryInterface() { defer reportPanic() switch i.(type) { case nil: fmt.Println("i is nil interface") case interface { m() }: fmt.Println("i has method m") } i.(interface { m() }).m() } func tryMap() { defer reportPanic() m[0] = 0 fmt.Println("m[0] =", m[0]) } func tryChannel() { defer reportPanic() close(c) fmt.Println("channel closed") }
Rewrite this program in C while keeping its functionality equivalent to the Factor version.
42 .
#include <stdio.h> #include <stdlib.h> int main() { int junk, *junkp; printf("junk: %d\n", junk); junkp = malloc(sizeof *junkp); if (junkp) printf("*junkp: %d\n", *junkp); return 0; }
Can you help me rewrite this code in C++ instead of Factor, keeping it the same logically?
42 .
#include <iostream> int main() { int undefined; if (undefined == 42) { std::cout << "42"; } if (undefined != 42) { std::cout << "not 42"; } }
Maintain the same structure and functionality when rewriting this code in Python.
42 .
try: name except NameError: print "name is undefined at first check" name = "Chocolate" try: name except NameError: print "name is undefined at second check" del name try: name except NameError: print "name is undefined at third check" name = 42 try: name except NameError: print "name is undefined at fourth check" print "Done"
Convert this Factor block to Go, preserving its control flow and logic.
42 .
package main import "fmt" var ( s []int p *int f func() i interface{} m map[int]int c chan int ) func main() { fmt.Println("Exercise nil objects:") status() s = make([]int, 1) p = &s[0] f = func() { fmt.Println("function call") } i = user(0) m = make(map[int]int) c = make(chan int, 1) fmt.Println("\nExercise objects after initialization:") status() } type user int func (user) m() { fmt.Println("method call") } func status() { trySlice() tryPointer() tryFunction() tryInterface() tryMap() tryChannel() } func reportPanic() { if x := recover(); x != nil { fmt.Println("panic:", x) } } func trySlice() { defer reportPanic() fmt.Println("s[0] =", s[0]) } func tryPointer() { defer reportPanic() fmt.Println("*p =", *p) } func tryFunction() { defer reportPanic() f() } func tryInterface() { defer reportPanic() switch i.(type) { case nil: fmt.Println("i is nil interface") case interface { m() }: fmt.Println("i has method m") } i.(interface { m() }).m() } func tryMap() { defer reportPanic() m[0] = 0 fmt.Println("m[0] =", m[0]) } func tryChannel() { defer reportPanic() close(c) fmt.Println("channel closed") }
Port the provided Fortran code into C++ while preserving the original functionality.
IsNaN(x)
#include <iostream> int main() { int undefined; if (undefined == 42) { std::cout << "42"; } if (undefined != 42) { std::cout << "not 42"; } }
Write the same algorithm in C as shown in this Fortran implementation.
IsNaN(x)
#include <stdio.h> #include <stdlib.h> int main() { int junk, *junkp; printf("junk: %d\n", junk); junkp = malloc(sizeof *junkp); if (junkp) printf("*junkp: %d\n", *junkp); return 0; }
Ensure the translated Go code behaves exactly like the original Fortran snippet.
IsNaN(x)
package main import "fmt" var ( s []int p *int f func() i interface{} m map[int]int c chan int ) func main() { fmt.Println("Exercise nil objects:") status() s = make([]int, 1) p = &s[0] f = func() { fmt.Println("function call") } i = user(0) m = make(map[int]int) c = make(chan int, 1) fmt.Println("\nExercise objects after initialization:") status() } type user int func (user) m() { fmt.Println("method call") } func status() { trySlice() tryPointer() tryFunction() tryInterface() tryMap() tryChannel() } func reportPanic() { if x := recover(); x != nil { fmt.Println("panic:", x) } } func trySlice() { defer reportPanic() fmt.Println("s[0] =", s[0]) } func tryPointer() { defer reportPanic() fmt.Println("*p =", *p) } func tryFunction() { defer reportPanic() f() } func tryInterface() { defer reportPanic() switch i.(type) { case nil: fmt.Println("i is nil interface") case interface { m() }: fmt.Println("i has method m") } i.(interface { m() }).m() } func tryMap() { defer reportPanic() m[0] = 0 fmt.Println("m[0] =", m[0]) } func tryChannel() { defer reportPanic() close(c) fmt.Println("channel closed") }
Maintain the same structure and functionality when rewriting this code in Python.
IsNaN(x)
try: name except NameError: print "name is undefined at first check" name = "Chocolate" try: name except NameError: print "name is undefined at second check" del name try: name except NameError: print "name is undefined at third check" name = 42 try: name except NameError: print "name is undefined at fourth check" print "Done"
Generate an equivalent PHP version of this Fortran code.
IsNaN(x)
<?php if (!isset($var)) echo "var is undefined at first check\n"; $var = "Chocolate"; if (!isset($var)) echo "var is undefined at second check\n"; unset($var); if (!isset($var)) echo "var is undefined at third check\n"; $var = 42; if (!isset($var)) echo "var is undefined at fourth check\n"; echo "Done\n"; ?>
Convert the following code from Haskell to C, ensuring the logic remains intact.
main = print $ "Incoming error
#include <stdio.h> #include <stdlib.h> int main() { int junk, *junkp; printf("junk: %d\n", junk); junkp = malloc(sizeof *junkp); if (junkp) printf("*junkp: %d\n", *junkp); return 0; }
Maintain the same structure and functionality when rewriting this code in C++.
main = print $ "Incoming error
#include <iostream> int main() { int undefined; if (undefined == 42) { std::cout << "42"; } if (undefined != 42) { std::cout << "not 42"; } }
Convert this Haskell block to Python, preserving its control flow and logic.
main = print $ "Incoming error
try: name except NameError: print "name is undefined at first check" name = "Chocolate" try: name except NameError: print "name is undefined at second check" del name try: name except NameError: print "name is undefined at third check" name = 42 try: name except NameError: print "name is undefined at fourth check" print "Done"
Please provide an equivalent version of this Haskell code in Go.
main = print $ "Incoming error
package main import "fmt" var ( s []int p *int f func() i interface{} m map[int]int c chan int ) func main() { fmt.Println("Exercise nil objects:") status() s = make([]int, 1) p = &s[0] f = func() { fmt.Println("function call") } i = user(0) m = make(map[int]int) c = make(chan int, 1) fmt.Println("\nExercise objects after initialization:") status() } type user int func (user) m() { fmt.Println("method call") } func status() { trySlice() tryPointer() tryFunction() tryInterface() tryMap() tryChannel() } func reportPanic() { if x := recover(); x != nil { fmt.Println("panic:", x) } } func trySlice() { defer reportPanic() fmt.Println("s[0] =", s[0]) } func tryPointer() { defer reportPanic() fmt.Println("*p =", *p) } func tryFunction() { defer reportPanic() f() } func tryInterface() { defer reportPanic() switch i.(type) { case nil: fmt.Println("i is nil interface") case interface { m() }: fmt.Println("i has method m") } i.(interface { m() }).m() } func tryMap() { defer reportPanic() m[0] = 0 fmt.Println("m[0] =", m[0]) } func tryChannel() { defer reportPanic() close(c) fmt.Println("channel closed") }
Write the same algorithm in C as shown in this J implementation.
foo=: 3 nc;:'foo bar' 0 _1
#include <stdio.h> #include <stdlib.h> int main() { int junk, *junkp; printf("junk: %d\n", junk); junkp = malloc(sizeof *junkp); if (junkp) printf("*junkp: %d\n", *junkp); return 0; }
Rewrite this program in C++ while keeping its functionality equivalent to the J version.
foo=: 3 nc;:'foo bar' 0 _1
#include <iostream> int main() { int undefined; if (undefined == 42) { std::cout << "42"; } if (undefined != 42) { std::cout << "not 42"; } }
Keep all operations the same but rewrite the snippet in Java.
foo=: 3 nc;:'foo bar' 0 _1
String string = null; System.out.println(string); System.out.println(string.length());
Ensure the translated Python code behaves exactly like the original J snippet.
foo=: 3 nc;:'foo bar' 0 _1
try: name except NameError: print "name is undefined at first check" name = "Chocolate" try: name except NameError: print "name is undefined at second check" del name try: name except NameError: print "name is undefined at third check" name = 42 try: name except NameError: print "name is undefined at fourth check" print "Done"
Write a version of this J function in Go with identical behavior.
foo=: 3 nc;:'foo bar' 0 _1
package main import "fmt" var ( s []int p *int f func() i interface{} m map[int]int c chan int ) func main() { fmt.Println("Exercise nil objects:") status() s = make([]int, 1) p = &s[0] f = func() { fmt.Println("function call") } i = user(0) m = make(map[int]int) c = make(chan int, 1) fmt.Println("\nExercise objects after initialization:") status() } type user int func (user) m() { fmt.Println("method call") } func status() { trySlice() tryPointer() tryFunction() tryInterface() tryMap() tryChannel() } func reportPanic() { if x := recover(); x != nil { fmt.Println("panic:", x) } } func trySlice() { defer reportPanic() fmt.Println("s[0] =", s[0]) } func tryPointer() { defer reportPanic() fmt.Println("*p =", *p) } func tryFunction() { defer reportPanic() f() } func tryInterface() { defer reportPanic() switch i.(type) { case nil: fmt.Println("i is nil interface") case interface { m() }: fmt.Println("i has method m") } i.(interface { m() }).m() } func tryMap() { defer reportPanic() m[0] = 0 fmt.Println("m[0] =", m[0]) } func tryChannel() { defer reportPanic() close(c) fmt.Println("channel closed") }
Convert this Julia block to C, preserving its control flow and logic.
julia> arr = [1, 2, nothing, 3] 4-element Array{Union{Nothing, Int64},1}: 1 2 nothing 3 julia> x = arr .+ 5 ERROR: MethodError: no method matching +(::Nothing, ::Int64) Closest candidates are: +(::Any, ::Any, ::Any, ::Any...) at operators.jl:502 +(::Complex{Bool}, ::Real) at complex.jl:292 +(::Missing, ::Number) at missing.jl:93 ... julia> arr = [1, 2, missing, 3] 4-element Array{Union{Missing, Int64},1}: 1 2 missing 3 julia> x = arr .+ 5 4-element Array{Union{Missing, Int64},1}: 6 7 missing 8
#include <stdio.h> #include <stdlib.h> int main() { int junk, *junkp; printf("junk: %d\n", junk); junkp = malloc(sizeof *junkp); if (junkp) printf("*junkp: %d\n", *junkp); return 0; }
Produce a functionally identical C# code for the snippet given in Julia.
julia> arr = [1, 2, nothing, 3] 4-element Array{Union{Nothing, Int64},1}: 1 2 nothing 3 julia> x = arr .+ 5 ERROR: MethodError: no method matching +(::Nothing, ::Int64) Closest candidates are: +(::Any, ::Any, ::Any, ::Any...) at operators.jl:502 +(::Complex{Bool}, ::Real) at complex.jl:292 +(::Missing, ::Number) at missing.jl:93 ... julia> arr = [1, 2, missing, 3] 4-element Array{Union{Missing, Int64},1}: 1 2 missing 3 julia> x = arr .+ 5 4-element Array{Union{Missing, Int64},1}: 6 7 missing 8
string foo = null;
Rewrite the snippet below in C++ so it works the same as the original Julia code.
julia> arr = [1, 2, nothing, 3] 4-element Array{Union{Nothing, Int64},1}: 1 2 nothing 3 julia> x = arr .+ 5 ERROR: MethodError: no method matching +(::Nothing, ::Int64) Closest candidates are: +(::Any, ::Any, ::Any, ::Any...) at operators.jl:502 +(::Complex{Bool}, ::Real) at complex.jl:292 +(::Missing, ::Number) at missing.jl:93 ... julia> arr = [1, 2, missing, 3] 4-element Array{Union{Missing, Int64},1}: 1 2 missing 3 julia> x = arr .+ 5 4-element Array{Union{Missing, Int64},1}: 6 7 missing 8
#include <iostream> int main() { int undefined; if (undefined == 42) { std::cout << "42"; } if (undefined != 42) { std::cout << "not 42"; } }
Translate the given Julia code snippet into Java without altering its behavior.
julia> arr = [1, 2, nothing, 3] 4-element Array{Union{Nothing, Int64},1}: 1 2 nothing 3 julia> x = arr .+ 5 ERROR: MethodError: no method matching +(::Nothing, ::Int64) Closest candidates are: +(::Any, ::Any, ::Any, ::Any...) at operators.jl:502 +(::Complex{Bool}, ::Real) at complex.jl:292 +(::Missing, ::Number) at missing.jl:93 ... julia> arr = [1, 2, missing, 3] 4-element Array{Union{Missing, Int64},1}: 1 2 missing 3 julia> x = arr .+ 5 4-element Array{Union{Missing, Int64},1}: 6 7 missing 8
String string = null; System.out.println(string); System.out.println(string.length());
Rewrite the snippet below in Python so it works the same as the original Julia code.
julia> arr = [1, 2, nothing, 3] 4-element Array{Union{Nothing, Int64},1}: 1 2 nothing 3 julia> x = arr .+ 5 ERROR: MethodError: no method matching +(::Nothing, ::Int64) Closest candidates are: +(::Any, ::Any, ::Any, ::Any...) at operators.jl:502 +(::Complex{Bool}, ::Real) at complex.jl:292 +(::Missing, ::Number) at missing.jl:93 ... julia> arr = [1, 2, missing, 3] 4-element Array{Union{Missing, Int64},1}: 1 2 missing 3 julia> x = arr .+ 5 4-element Array{Union{Missing, Int64},1}: 6 7 missing 8
try: name except NameError: print "name is undefined at first check" name = "Chocolate" try: name except NameError: print "name is undefined at second check" del name try: name except NameError: print "name is undefined at third check" name = 42 try: name except NameError: print "name is undefined at fourth check" print "Done"
Preserve the algorithm and functionality while converting the code from Julia to Go.
julia> arr = [1, 2, nothing, 3] 4-element Array{Union{Nothing, Int64},1}: 1 2 nothing 3 julia> x = arr .+ 5 ERROR: MethodError: no method matching +(::Nothing, ::Int64) Closest candidates are: +(::Any, ::Any, ::Any, ::Any...) at operators.jl:502 +(::Complex{Bool}, ::Real) at complex.jl:292 +(::Missing, ::Number) at missing.jl:93 ... julia> arr = [1, 2, missing, 3] 4-element Array{Union{Missing, Int64},1}: 1 2 missing 3 julia> x = arr .+ 5 4-element Array{Union{Missing, Int64},1}: 6 7 missing 8
package main import "fmt" var ( s []int p *int f func() i interface{} m map[int]int c chan int ) func main() { fmt.Println("Exercise nil objects:") status() s = make([]int, 1) p = &s[0] f = func() { fmt.Println("function call") } i = user(0) m = make(map[int]int) c = make(chan int, 1) fmt.Println("\nExercise objects after initialization:") status() } type user int func (user) m() { fmt.Println("method call") } func status() { trySlice() tryPointer() tryFunction() tryInterface() tryMap() tryChannel() } func reportPanic() { if x := recover(); x != nil { fmt.Println("panic:", x) } } func trySlice() { defer reportPanic() fmt.Println("s[0] =", s[0]) } func tryPointer() { defer reportPanic() fmt.Println("*p =", *p) } func tryFunction() { defer reportPanic() f() } func tryInterface() { defer reportPanic() switch i.(type) { case nil: fmt.Println("i is nil interface") case interface { m() }: fmt.Println("i has method m") } i.(interface { m() }).m() } func tryMap() { defer reportPanic() m[0] = 0 fmt.Println("m[0] =", m[0]) } func tryChannel() { defer reportPanic() close(c) fmt.Println("channel closed") }
Generate a C translation of this Lua snippet without changing its computational steps.
print( a ) local b print( b ) if b == nil then b = 5 end print( b )
#include <stdio.h> #include <stdlib.h> int main() { int junk, *junkp; printf("junk: %d\n", junk); junkp = malloc(sizeof *junkp); if (junkp) printf("*junkp: %d\n", *junkp); return 0; }
Convert this Lua snippet to C# and keep its semantics consistent.
print( a ) local b print( b ) if b == nil then b = 5 end print( b )
string foo = null;
Write a version of this Lua function in C++ with identical behavior.
print( a ) local b print( b ) if b == nil then b = 5 end print( b )
#include <iostream> int main() { int undefined; if (undefined == 42) { std::cout << "42"; } if (undefined != 42) { std::cout << "not 42"; } }
Preserve the algorithm and functionality while converting the code from Lua to Java.
print( a ) local b print( b ) if b == nil then b = 5 end print( b )
String string = null; System.out.println(string); System.out.println(string.length());
Preserve the algorithm and functionality while converting the code from Lua to Python.
print( a ) local b print( b ) if b == nil then b = 5 end print( b )
try: name except NameError: print "name is undefined at first check" name = "Chocolate" try: name except NameError: print "name is undefined at second check" del name try: name except NameError: print "name is undefined at third check" name = 42 try: name except NameError: print "name is undefined at fourth check" print "Done"
Ensure the translated Go code behaves exactly like the original Lua snippet.
print( a ) local b print( b ) if b == nil then b = 5 end print( b )
package main import "fmt" var ( s []int p *int f func() i interface{} m map[int]int c chan int ) func main() { fmt.Println("Exercise nil objects:") status() s = make([]int, 1) p = &s[0] f = func() { fmt.Println("function call") } i = user(0) m = make(map[int]int) c = make(chan int, 1) fmt.Println("\nExercise objects after initialization:") status() } type user int func (user) m() { fmt.Println("method call") } func status() { trySlice() tryPointer() tryFunction() tryInterface() tryMap() tryChannel() } func reportPanic() { if x := recover(); x != nil { fmt.Println("panic:", x) } } func trySlice() { defer reportPanic() fmt.Println("s[0] =", s[0]) } func tryPointer() { defer reportPanic() fmt.Println("*p =", *p) } func tryFunction() { defer reportPanic() f() } func tryInterface() { defer reportPanic() switch i.(type) { case nil: fmt.Println("i is nil interface") case interface { m() }: fmt.Println("i has method m") } i.(interface { m() }).m() } func tryMap() { defer reportPanic() m[0] = 0 fmt.Println("m[0] =", m[0]) } func tryChannel() { defer reportPanic() close(c) fmt.Println("channel closed") }
Translate the given Mathematica code snippet into C without altering its behavior.
a -> a a + a -> 2 a ValueQ[a] -> False a = 5 -> 5 ValueQ[a] -> True
#include <stdio.h> #include <stdlib.h> int main() { int junk, *junkp; printf("junk: %d\n", junk); junkp = malloc(sizeof *junkp); if (junkp) printf("*junkp: %d\n", *junkp); return 0; }
Translate the given Mathematica code snippet into C# without altering its behavior.
a -> a a + a -> 2 a ValueQ[a] -> False a = 5 -> 5 ValueQ[a] -> True
string foo = null;
Maintain the same structure and functionality when rewriting this code in C++.
a -> a a + a -> 2 a ValueQ[a] -> False a = 5 -> 5 ValueQ[a] -> True
#include <iostream> int main() { int undefined; if (undefined == 42) { std::cout << "42"; } if (undefined != 42) { std::cout << "not 42"; } }
Keep all operations the same but rewrite the snippet in Java.
a -> a a + a -> 2 a ValueQ[a] -> False a = 5 -> 5 ValueQ[a] -> True
String string = null; System.out.println(string); System.out.println(string.length());
Rewrite this program in Python while keeping its functionality equivalent to the Mathematica version.
a -> a a + a -> 2 a ValueQ[a] -> False a = 5 -> 5 ValueQ[a] -> True
try: name except NameError: print "name is undefined at first check" name = "Chocolate" try: name except NameError: print "name is undefined at second check" del name try: name except NameError: print "name is undefined at third check" name = 42 try: name except NameError: print "name is undefined at fourth check" print "Done"
Transform the following Mathematica implementation into Go, maintaining the same output and logic.
a -> a a + a -> 2 a ValueQ[a] -> False a = 5 -> 5 ValueQ[a] -> True
package main import "fmt" var ( s []int p *int f func() i interface{} m map[int]int c chan int ) func main() { fmt.Println("Exercise nil objects:") status() s = make([]int, 1) p = &s[0] f = func() { fmt.Println("function call") } i = user(0) m = make(map[int]int) c = make(chan int, 1) fmt.Println("\nExercise objects after initialization:") status() } type user int func (user) m() { fmt.Println("method call") } func status() { trySlice() tryPointer() tryFunction() tryInterface() tryMap() tryChannel() } func reportPanic() { if x := recover(); x != nil { fmt.Println("panic:", x) } } func trySlice() { defer reportPanic() fmt.Println("s[0] =", s[0]) } func tryPointer() { defer reportPanic() fmt.Println("*p =", *p) } func tryFunction() { defer reportPanic() f() } func tryInterface() { defer reportPanic() switch i.(type) { case nil: fmt.Println("i is nil interface") case interface { m() }: fmt.Println("i has method m") } i.(interface { m() }).m() } func tryMap() { defer reportPanic() m[0] = 0 fmt.Println("m[0] =", m[0]) } func tryChannel() { defer reportPanic() close(c) fmt.Println("channel closed") }
Can you help me rewrite this code in C++ instead of MATLAB, keeping it the same logically?
global var;
#include <iostream> int main() { int undefined; if (undefined == 42) { std::cout << "42"; } if (undefined != 42) { std::cout << "not 42"; } }
Generate a Python translation of this MATLAB snippet without changing its computational steps.
global var;
try: name except NameError: print "name is undefined at first check" name = "Chocolate" try: name except NameError: print "name is undefined at second check" del name try: name except NameError: print "name is undefined at third check" name = 42 try: name except NameError: print "name is undefined at fourth check" print "Done"
Keep all operations the same but rewrite the snippet in Go.
global var;
package main import "fmt" var ( s []int p *int f func() i interface{} m map[int]int c chan int ) func main() { fmt.Println("Exercise nil objects:") status() s = make([]int, 1) p = &s[0] f = func() { fmt.Println("function call") } i = user(0) m = make(map[int]int) c = make(chan int, 1) fmt.Println("\nExercise objects after initialization:") status() } type user int func (user) m() { fmt.Println("method call") } func status() { trySlice() tryPointer() tryFunction() tryInterface() tryMap() tryChannel() } func reportPanic() { if x := recover(); x != nil { fmt.Println("panic:", x) } } func trySlice() { defer reportPanic() fmt.Println("s[0] =", s[0]) } func tryPointer() { defer reportPanic() fmt.Println("*p =", *p) } func tryFunction() { defer reportPanic() f() } func tryInterface() { defer reportPanic() switch i.(type) { case nil: fmt.Println("i is nil interface") case interface { m() }: fmt.Println("i has method m") } i.(interface { m() }).m() } func tryMap() { defer reportPanic() m[0] = 0 fmt.Println("m[0] =", m[0]) } func tryChannel() { defer reportPanic() close(c) fmt.Println("channel closed") }
Generate a C translation of this Nim snippet without changing its computational steps.
var a {.noInit.}: array[1_000_000, int] proc p(): array[1000, int] {.noInit.} = for i in 0..999: result[i] = i
#include <stdio.h> #include <stdlib.h> int main() { int junk, *junkp; printf("junk: %d\n", junk); junkp = malloc(sizeof *junkp); if (junkp) printf("*junkp: %d\n", *junkp); return 0; }
Translate the given Nim code snippet into C# without altering its behavior.
var a {.noInit.}: array[1_000_000, int] proc p(): array[1000, int] {.noInit.} = for i in 0..999: result[i] = i
string foo = null;
Write the same algorithm in Java as shown in this Nim implementation.
var a {.noInit.}: array[1_000_000, int] proc p(): array[1000, int] {.noInit.} = for i in 0..999: result[i] = i
String string = null; System.out.println(string); System.out.println(string.length());
Translate the given Nim code snippet into Python without altering its behavior.
var a {.noInit.}: array[1_000_000, int] proc p(): array[1000, int] {.noInit.} = for i in 0..999: result[i] = i
try: name except NameError: print "name is undefined at first check" name = "Chocolate" try: name except NameError: print "name is undefined at second check" del name try: name except NameError: print "name is undefined at third check" name = 42 try: name except NameError: print "name is undefined at fourth check" print "Done"
Change the programming language of this snippet from Nim to Go without modifying what it does.
var a {.noInit.}: array[1_000_000, int] proc p(): array[1000, int] {.noInit.} = for i in 0..999: result[i] = i
package main import "fmt" var ( s []int p *int f func() i interface{} m map[int]int c chan int ) func main() { fmt.Println("Exercise nil objects:") status() s = make([]int, 1) p = &s[0] f = func() { fmt.Println("function call") } i = user(0) m = make(map[int]int) c = make(chan int, 1) fmt.Println("\nExercise objects after initialization:") status() } type user int func (user) m() { fmt.Println("method call") } func status() { trySlice() tryPointer() tryFunction() tryInterface() tryMap() tryChannel() } func reportPanic() { if x := recover(); x != nil { fmt.Println("panic:", x) } } func trySlice() { defer reportPanic() fmt.Println("s[0] =", s[0]) } func tryPointer() { defer reportPanic() fmt.Println("*p =", *p) } func tryFunction() { defer reportPanic() f() } func tryInterface() { defer reportPanic() switch i.(type) { case nil: fmt.Println("i is nil interface") case interface { m() }: fmt.Println("i has method m") } i.(interface { m() }).m() } func tryMap() { defer reportPanic() m[0] = 0 fmt.Println("m[0] =", m[0]) } func tryChannel() { defer reportPanic() close(c) fmt.Println("channel closed") }
Rewrite the snippet below in C so it works the same as the original OCaml code.
let inc = function Some n -> Some (n+1) | None -> failwith "Undefined argument";; inc (Some 0);; inc None;;
#include <stdio.h> #include <stdlib.h> int main() { int junk, *junkp; printf("junk: %d\n", junk); junkp = malloc(sizeof *junkp); if (junkp) printf("*junkp: %d\n", *junkp); return 0; }
Generate a C# translation of this OCaml snippet without changing its computational steps.
let inc = function Some n -> Some (n+1) | None -> failwith "Undefined argument";; inc (Some 0);; inc None;;
string foo = null;
Write a version of this OCaml function in C++ with identical behavior.
let inc = function Some n -> Some (n+1) | None -> failwith "Undefined argument";; inc (Some 0);; inc None;;
#include <iostream> int main() { int undefined; if (undefined == 42) { std::cout << "42"; } if (undefined != 42) { std::cout << "not 42"; } }
Port the provided OCaml code into Java while preserving the original functionality.
let inc = function Some n -> Some (n+1) | None -> failwith "Undefined argument";; inc (Some 0);; inc None;;
String string = null; System.out.println(string); System.out.println(string.length());
Write a version of this OCaml function in Python with identical behavior.
let inc = function Some n -> Some (n+1) | None -> failwith "Undefined argument";; inc (Some 0);; inc None;;
try: name except NameError: print "name is undefined at first check" name = "Chocolate" try: name except NameError: print "name is undefined at second check" del name try: name except NameError: print "name is undefined at third check" name = 42 try: name except NameError: print "name is undefined at fourth check" print "Done"
Port the provided OCaml code into Go while preserving the original functionality.
let inc = function Some n -> Some (n+1) | None -> failwith "Undefined argument";; inc (Some 0);; inc None;;
package main import "fmt" var ( s []int p *int f func() i interface{} m map[int]int c chan int ) func main() { fmt.Println("Exercise nil objects:") status() s = make([]int, 1) p = &s[0] f = func() { fmt.Println("function call") } i = user(0) m = make(map[int]int) c = make(chan int, 1) fmt.Println("\nExercise objects after initialization:") status() } type user int func (user) m() { fmt.Println("method call") } func status() { trySlice() tryPointer() tryFunction() tryInterface() tryMap() tryChannel() } func reportPanic() { if x := recover(); x != nil { fmt.Println("panic:", x) } } func trySlice() { defer reportPanic() fmt.Println("s[0] =", s[0]) } func tryPointer() { defer reportPanic() fmt.Println("*p =", *p) } func tryFunction() { defer reportPanic() f() } func tryInterface() { defer reportPanic() switch i.(type) { case nil: fmt.Println("i is nil interface") case interface { m() }: fmt.Println("i has method m") } i.(interface { m() }).m() } func tryMap() { defer reportPanic() m[0] = 0 fmt.Println("m[0] =", m[0]) } func tryChannel() { defer reportPanic() close(c) fmt.Println("channel closed") }
Generate an equivalent C version of this Perl code.
use strict; our $var; print "var contains an undefined value at first check\n" unless defined $var; $var = "Chocolate"; print "var contains an undefined value at second check\n" unless defined $var; $var = undef; undef($var); print "var contains an undefined value at third check\n" unless defined $var; $var = 42; print "var contains an undefined value at fourth check\n" unless defined $var; print "Done\n";
#include <stdio.h> #include <stdlib.h> int main() { int junk, *junkp; printf("junk: %d\n", junk); junkp = malloc(sizeof *junkp); if (junkp) printf("*junkp: %d\n", *junkp); return 0; }
Please provide an equivalent version of this Perl code in C#.
use strict; our $var; print "var contains an undefined value at first check\n" unless defined $var; $var = "Chocolate"; print "var contains an undefined value at second check\n" unless defined $var; $var = undef; undef($var); print "var contains an undefined value at third check\n" unless defined $var; $var = 42; print "var contains an undefined value at fourth check\n" unless defined $var; print "Done\n";
string foo = null;
Produce a functionally identical C++ code for the snippet given in Perl.
use strict; our $var; print "var contains an undefined value at first check\n" unless defined $var; $var = "Chocolate"; print "var contains an undefined value at second check\n" unless defined $var; $var = undef; undef($var); print "var contains an undefined value at third check\n" unless defined $var; $var = 42; print "var contains an undefined value at fourth check\n" unless defined $var; print "Done\n";
#include <iostream> int main() { int undefined; if (undefined == 42) { std::cout << "42"; } if (undefined != 42) { std::cout << "not 42"; } }
Translate the given Perl code snippet into Java without altering its behavior.
use strict; our $var; print "var contains an undefined value at first check\n" unless defined $var; $var = "Chocolate"; print "var contains an undefined value at second check\n" unless defined $var; $var = undef; undef($var); print "var contains an undefined value at third check\n" unless defined $var; $var = 42; print "var contains an undefined value at fourth check\n" unless defined $var; print "Done\n";
String string = null; System.out.println(string); System.out.println(string.length());
Write the same algorithm in Python as shown in this Perl implementation.
use strict; our $var; print "var contains an undefined value at first check\n" unless defined $var; $var = "Chocolate"; print "var contains an undefined value at second check\n" unless defined $var; $var = undef; undef($var); print "var contains an undefined value at third check\n" unless defined $var; $var = 42; print "var contains an undefined value at fourth check\n" unless defined $var; print "Done\n";
try: name except NameError: print "name is undefined at first check" name = "Chocolate" try: name except NameError: print "name is undefined at second check" del name try: name except NameError: print "name is undefined at third check" name = 42 try: name except NameError: print "name is undefined at fourth check" print "Done"
Convert this Perl block to Go, preserving its control flow and logic.
use strict; our $var; print "var contains an undefined value at first check\n" unless defined $var; $var = "Chocolate"; print "var contains an undefined value at second check\n" unless defined $var; $var = undef; undef($var); print "var contains an undefined value at third check\n" unless defined $var; $var = 42; print "var contains an undefined value at fourth check\n" unless defined $var; print "Done\n";
package main import "fmt" var ( s []int p *int f func() i interface{} m map[int]int c chan int ) func main() { fmt.Println("Exercise nil objects:") status() s = make([]int, 1) p = &s[0] f = func() { fmt.Println("function call") } i = user(0) m = make(map[int]int) c = make(chan int, 1) fmt.Println("\nExercise objects after initialization:") status() } type user int func (user) m() { fmt.Println("method call") } func status() { trySlice() tryPointer() tryFunction() tryInterface() tryMap() tryChannel() } func reportPanic() { if x := recover(); x != nil { fmt.Println("panic:", x) } } func trySlice() { defer reportPanic() fmt.Println("s[0] =", s[0]) } func tryPointer() { defer reportPanic() fmt.Println("*p =", *p) } func tryFunction() { defer reportPanic() f() } func tryInterface() { defer reportPanic() switch i.(type) { case nil: fmt.Println("i is nil interface") case interface { m() }: fmt.Println("i has method m") } i.(interface { m() }).m() } func tryMap() { defer reportPanic() m[0] = 0 fmt.Println("m[0] =", m[0]) } func tryChannel() { defer reportPanic() close(c) fmt.Println("channel closed") }
Port the following code from PowerShell to C with equivalent syntax and logic.
if (Get-Variable -Name noSuchVariable -ErrorAction SilentlyContinue) { $true } else { $false }
#include <stdio.h> #include <stdlib.h> int main() { int junk, *junkp; printf("junk: %d\n", junk); junkp = malloc(sizeof *junkp); if (junkp) printf("*junkp: %d\n", *junkp); return 0; }
Generate a C# translation of this PowerShell snippet without changing its computational steps.
if (Get-Variable -Name noSuchVariable -ErrorAction SilentlyContinue) { $true } else { $false }
string foo = null;
Change the programming language of this snippet from PowerShell to C++ without modifying what it does.
if (Get-Variable -Name noSuchVariable -ErrorAction SilentlyContinue) { $true } else { $false }
#include <iostream> int main() { int undefined; if (undefined == 42) { std::cout << "42"; } if (undefined != 42) { std::cout << "not 42"; } }
Keep all operations the same but rewrite the snippet in Java.
if (Get-Variable -Name noSuchVariable -ErrorAction SilentlyContinue) { $true } else { $false }
String string = null; System.out.println(string); System.out.println(string.length());
Translate this program into Python but keep the logic exactly as in PowerShell.
if (Get-Variable -Name noSuchVariable -ErrorAction SilentlyContinue) { $true } else { $false }
try: name except NameError: print "name is undefined at first check" name = "Chocolate" try: name except NameError: print "name is undefined at second check" del name try: name except NameError: print "name is undefined at third check" name = 42 try: name except NameError: print "name is undefined at fourth check" print "Done"
Port the provided PowerShell code into Go while preserving the original functionality.
if (Get-Variable -Name noSuchVariable -ErrorAction SilentlyContinue) { $true } else { $false }
package main import "fmt" var ( s []int p *int f func() i interface{} m map[int]int c chan int ) func main() { fmt.Println("Exercise nil objects:") status() s = make([]int, 1) p = &s[0] f = func() { fmt.Println("function call") } i = user(0) m = make(map[int]int) c = make(chan int, 1) fmt.Println("\nExercise objects after initialization:") status() } type user int func (user) m() { fmt.Println("method call") } func status() { trySlice() tryPointer() tryFunction() tryInterface() tryMap() tryChannel() } func reportPanic() { if x := recover(); x != nil { fmt.Println("panic:", x) } } func trySlice() { defer reportPanic() fmt.Println("s[0] =", s[0]) } func tryPointer() { defer reportPanic() fmt.Println("*p =", *p) } func tryFunction() { defer reportPanic() f() } func tryInterface() { defer reportPanic() switch i.(type) { case nil: fmt.Println("i is nil interface") case interface { m() }: fmt.Println("i has method m") } i.(interface { m() }).m() } func tryMap() { defer reportPanic() m[0] = 0 fmt.Println("m[0] =", m[0]) } func tryChannel() { defer reportPanic() close(c) fmt.Println("channel closed") }
Produce a functionally identical C code for the snippet given in R.
exists("x")
#include <stdio.h> #include <stdlib.h> int main() { int junk, *junkp; printf("junk: %d\n", junk); junkp = malloc(sizeof *junkp); if (junkp) printf("*junkp: %d\n", *junkp); return 0; }
Write a version of this R function in C++ with identical behavior.
exists("x")
#include <iostream> int main() { int undefined; if (undefined == 42) { std::cout << "42"; } if (undefined != 42) { std::cout << "not 42"; } }