What do all those permissions mean? - file-permissions

Where can I find out the meaning?

r = read
w = write
x = execute
1st block of rwx is for owner
2nd block for group
3rd for public

Related

initialize an argument of a proc, but not in it's definition line

Suppose that we have an object which has some properties of type proc:
type
x = object
y: proc(a,b:int)
proc myproc(a,b:int) =
echo a
var tmp = new x
tmp.y = myproc # I want to insert initial value in this line for example a = 1
tmp.y(5)
How can I insert initial values in the specified line, and not anywhere else?
Thank you in advance
As far as I know it's not possible to do what you want without other modifications, because you have specified y to be a proc receiving two parameters. So whatever you assign to it, the compiler is always going to expect you to put two parameters at the call site.
One alternate approach would be to use default values in the proc definition:
type
x = object
y: proc(a: int = 1, b: int)
proc myproc(a,b: int) =
echo(a, " something ", b)
var tmp = new x
tmp.y = myproc
tmp.y(b = 5)
The problems with this solution are of course that you can't change the value of a at runtime, and you are forced to manually specify the name of the parameter, otherwise the compiler is going to presume you are meaning the first and forgot to specify b. Such is the life of a non dynamic language.
Another approach is to define the proc as having a single input parameter, and then using an anonymous proc or lambda to curry whatever values you want:
type
x = object
y: proc(a: int)
proc myproc(a,b: int) =
echo(a, " something ", b)
var tmp = new x
tmp.y = proc (x: int) = myproc(1, x)
tmp.y(5)
If you were to use the sugar module, as suggested in the docs, the assignment line could look like:
tmp.y = (x: int) => myproc(1, x)

The runtime constant in forward declaration

How do we do runtime constant in forward definition/declaration by any way around (tried hard not work on using let)
let n :int
proc m : int =
let i=1
var u=n+i
n=m()
error for this or for other varied ones:
Error: 'let' symbol requires an initialization
Error: redefinition of 'n'; previous
... etc
Please solve it out, thanks before
It's hard to say what you are trying to do with this code, a let variable is treated as a constant, so it can't be modified, but presumably your intent is to use the variable as a sort of counter to increase itself and presumably reassign n multiple times by calling m()? If you remove the n from the proc you can write the code like:
proc m : int =
let i=1
var u=0+i
let n :int = m()
So if you actually want n to be mutable, there's no problem using a var:
var n :int
proc m : int =
let i=1
var u=n+i
u
n=m()
echo n
Note that I had to add u as a last line to the m proc, because otherwise you were returning nothing and the assignment to n would always be zero, which is the default value for the implicit result variable inside a proc that returns something. You can verify this by repeating the n=m() assignment before the last echo.

How to use 'aux' keyword in Haskell

Okay, I've looked on about 4-5 websites that offered to teach Haskell and not one of them explained the keyword aux. They just started using it. I've only really studied Java and C (never saw it in either if it exists), and I've never really encountered it before this class that I'm taking on Haskell. All I can really tell is that it provides the utility to create and store a value within a function. So what exactly does it do and how is it properly used and formatted? In particular, could you explain its use while recursing? I don't think that its use is any different, but just to make sure I thought I would ask.
There is no keyword aux, my guess is this is just the name they used for a local function.
Just like you can define top-level values:
myValue = 4
or top-level functions:
myFunction x = 2 * x
you can similarly define local values:
myValue =
let myLocalValue = 3 in
myLocalValue + 1
-- or equivalently:
myValue = myLocalValue + 1
where myLocalValue = 3
or a local function:
myValue =
let myLocalFunction x = 2 * x in
myLocalFunction 2
-- or equivalently:
myValue = myLocalFunction 2
where myLocalFunction x = 2 * x
Your teacher simply named the local function aux instead of myLocalFunction.

Convert string to a variable name

I am using R to parse a list of strings in the form:
original_string <- "variable_name=variable_value"
First, I extract the variable name and value from the original string and convert the value to numeric class.
parameter_value <- as.numeric("variable_value")
parameter_name <- "variable_name"
Then, I would like to assign the value to a variable with the same name as the parameter_name string.
variable_name <- parameter_value
What is/are the function(s) for doing this?
assign is what you are looking for.
assign("x", 5)
x
[1] 5
but buyer beware.
See R FAQ 7.21
http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f
You can use do.call:
do.call("<-",list(parameter_name, parameter_value))
There is another simple solution found there:
http://www.r-bloggers.com/converting-a-string-to-a-variable-name-on-the-fly-and-vice-versa-in-r/
To convert a string to a variable:
x <- 42
eval(parse(text = "x"))
[1] 42
And the opposite:
x <- 42
deparse(substitute(x))
[1] "x"
The function you are looking for is get():
assign ("abc",5)
get("abc")
Confirming that the memory address is identical:
getabc <- get("abc")
pryr::address(abc) == pryr::address(getabc)
# [1] TRUE
Reference: R FAQ 7.21 How can I turn a string into a variable?
Use x=as.name("string"). You can use then use x to refer to the variable with name string.
I don't know, if it answers your question correctly.
strsplit to parse your input and, as Greg mentioned, assign to assign the variables.
original_string <- c("x=123", "y=456")
pairs <- strsplit(original_string, "=")
lapply(pairs, function(x) assign(x[1], as.numeric(x[2]), envir = globalenv()))
ls()
assign is good, but I have not found a function for referring back to the variable you've created in an automated script. (as.name seems to work the opposite way). More experienced coders will doubtless have a better solution, but this solution works and is slightly humorous perhaps, in that it gets R to write code for itself to execute.
Say I have just assigned value 5 to x (var.name <- "x"; assign(var.name, 5)) and I want to change the value to 6. If I am writing a script and don't know in advance what the variable name (var.name) will be (which seems to be the point of the assign function), I can't simply put x <- 6 because var.name might have been "y". So I do:
var.name <- "x"
#some other code...
assign(var.name, 5)
#some more code...
#write a script file (1 line in this case) that works with whatever variable name
write(paste0(var.name, " <- 6"), "tmp.R")
#source that script file
source("tmp.R")
#remove the script file for tidiness
file.remove("tmp.R")
x will be changed to 6, and if the variable name was anything other than "x", that variable will similarly have been changed to 6.
I was working with this a few days ago, and noticed that sometimes you will need to use the get() function to print the results of your variable.
ie :
varnames = c('jan', 'feb', 'march')
file_names = list_files('path to multiple csv files saved on drive')
assign(varnames[1], read.csv(file_names[1]) # This will assign the variable
From there, if you try to print the variable varnames[1], it returns 'jan'.
To work around this, you need to do
print(get(varnames[1]))
If you want to convert string to variable inside body of function, but you want to have variable global:
test <- function() {
do.call("<<-",list("vartest","xxx"))
}
test()
vartest
[1] "xxx"
Maybe I didn't understand your problem right, because of the simplicity of your example. To my understanding, you have a series of instructions stored in character vectors, and those instructions are very close to being properly formatted, except that you'd like to cast the right member to numeric.
If my understanding is right, I would like to propose a slightly different approach, that does not rely on splitting your original string, but directly evaluates your instruction (with a little improvement).
original_string <- "variable_name=\"10\"" # Your original instruction, but with an actual numeric on the right, stored as character.
library(magrittr) # Or library(tidyverse), but it seems a bit overkilled if the point is just to import pipe-stream operator
eval(parse(text=paste(eval(original_string), "%>% as.numeric")))
print(variable_name)
#[1] 10
Basically, what we are doing is that we 'improve' your instruction variable_name="10" so that it becomes variable_name="10" %>% as.numeric, which is an equivalent of variable_name=as.numeric("10") with magrittr pipe-stream syntax. Then we evaluate this expression within current environment.
Hope that helps someone who'd wander around here 8 years later ;-)
Other than assign, one other way to assign value to string named object is to access .GlobalEnv directly.
# Equivalent
assign('abc',3)
.GlobalEnv$'abc' = 3
Accessing .GlobalEnv gives some flexibility, and my use case was assigning values to a string-named list. For example,
.GlobalEnv$'x' = list()
.GlobalEnv$'x'[[2]] = 5 # works
var = 'x'
.GlobalEnv[[glue::glue('{var}')]][[2]] = 5 # programmatic names from glue()

Haskell Relations Error - Syntax error in declaration (unexpected `;', possibly due to bad layout)

I'm fairly new to Haskell and I don't fully understand this error, when I load the file hugs prints out the following "Syntax error in declaration (unexpected `;', possibly due to bad layout)" at the line "check s1 s2 ((x,y):xs)". I find this confusing as there isn't a ";" in the code. If someone could explain why this is happening and how I can fix it I would be very grateful. Bellow is my code.
type Owned = String
type Owner = String
type Fact = (Owned,Owner)
database = [(String, String)]
database = [("c4","c5"),("c1","c2"), ("c2", "c3"), ("c3","c4")]
owns :: Owner -> Owned -> Bool
owns s1 s2
| check s1 s2 database = true
| otherwise false
check s1 s2 ((x,y):xs)
| s1==x && y==s2 = true
| s1==x && y==s2 = (check y s2 database)
| otherwise false
You are missing an = in the otherwise branches:
type Owned = String
type Owner = String
type Fact = (Owned,Owner)
database = [(String, String)]
database = [("c4","c5"),("c1","c2"), ("c2", "c3"), ("c3","c4")]
owns :: Owner -> Owned -> Bool
owns s1 s2
| check s1 s2 database = true
| otherwise = false
check s1 s2 ((x,y):xs)
| s1==x && y==s2 = true
| s1==x && y==s2 = (check y s2 database)
| otherwise = false
Actually, there are ; in the transformed source code. The Haskell Report contains a detailed explanation about how the source code is transformed with the layout rules. One should read them once, it's quite intuitive.
Though such errors are indeed confusing for a newcomer, the following rules of thumb apply:
If the compiler complains about ';' you have a syntax error in the last nonempty line before the line reported.
More often than not, the error comes about due to layout (as indeed the compiler suggested). Nevertheless, there are other cases as your example shows: the compiler desperately looks for a '=' in the second guard of function owns and when he finds the semicolon just before check he knows there is something wrong.

Resources