Last active
January 14, 2016 23:29
-
-
Save jbranchaud/2d512ca2c4843c754ead to your computer and use it in GitHub Desktop.
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
!! EASY | |
! Using only * and +, how would you calculate 3^2 + 4^2 with Factor? | |
3 3 * 4 4 * + | |
clear | |
! Enter USE: math.functions in the Listener. Now, with sq and sqrt, | |
! calculate the square root of 3^2 + 4^2. | |
USE: math.functions | |
3 sq 4 sq + sqrt | |
clear | |
! If you had the numbers 1 2 on the stack, what code could you use to end up | |
! with 1 1 2 on the stack? | |
1 2 over swap | |
clear | |
! Enter USE: ascii in the Listener. Put your name on the stack, and write a | |
! line of code that puts "Hello," in front of your name and converts the whole | |
! string to uppercase. Use the append word to concatenate two strings and | |
! >upper to convert to uppercase. Did you have to do any stack shuffling to | |
! get the desired result? | |
"Josh" | |
"Hello, " swap append >upper | |
clear | |
!! MEDIUM | |
! The reduce word taks a sequence, an intial value, and a quotation and | |
! returns the result of applying the quotation to the initial value and the | |
! first element of the sequence, then the result of applying th quotation to | |
! the result and th next element of the sequence, and so on. Using reduce, | |
! write a line of code that returns the sum of the numbers 1, 4, 17, 9, 11. | |
! Try it out on your own first, but if you are truly stuck, look back | |
! carefully over the pages you've just read. There is a hint hiding somewhere. | |
{ 1 4 17 9 11 } 0 [ + ] reduce ! -> 42 | |
clear | |
! Now calculate the sum of the numbers 1 to 100 in a similar fashion. Do not | |
! manually write the sequence of numbers. Instead, enter USE: math.ranges in | |
! the Listener, and use the [1,b] word to produce the sequence. | |
USE: math.ranges | |
100 [1,b] 0 [ + ] reduce ! -> 5050 | |
clear | |
! The map word takes a sequence and a quotation, and returns a sequence of | |
! results of applying the quotation to each value. Using map and the words | |
! that you have learned so far, write a line of code that returns the squares | |
! of the numbers 1 to 10. | |
10 [1,b] [ sq ] map ! -> { 1 4 9 16 25 36 49 64 81 100 } | |
clear | |
!! HARD | |
! Write a line of code that, given a number between 1 and 99, returns the | |
! two digits in the number. That is, given 42 <your code>, you should get 4 | |
! and 2 on the stack. Use the words /i, mod, and bi to accomplish the task. | |
42 [ 10 /i ] [ 10 mod ] bi | |
! Repeat the previous exercise for any number of digits. Use a different | |
! strategy, though: first convert the number to a string, then iterate over | |
! each character, converting each character back to a string and then to a | |
! number. Enter USE: math.parser in the Listener and use number>string, | |
! string>number, 1string, and each. | |
USE: math.parser | |
42 number>string [ 1string ] each [ string>number ] bi@ | |
clear |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment