How to use multiple 'commands' in the same function in Haskell - function

I'm trying to figure out how, if even possible, can you perform more than one command within a Haskell function? For example, if I had:
foo [[a]] = print (head a) --AND -- map (head of everything but the first value)
How would I go about doing something like that? Is there a function which I could use in place of --AND-- which would allow me to perform both commands on the list of lists?

You're probably looking for do notation, which combined with properly formed pattern matching would get your desired behavior:
foo :: Show a => [[a]] -> IO ()
foo xs = do
print $ head $ head xs -- first value in the 2D list
print $ map head $ tail xs -- Skip first sublist with tail

Control.Arrow module has many combinators for wiring inputs to functions in interesting ways. Fanouts, fan-ins, etc.
On my phone, but something like print (<<<) head (&&&) map ( f . tail) $ input . That is wrong functions and order of operations, but gives a hint of the flavor. Check the module docs.

Related

Printing variables including functions from Makefile and/or variable introspection

If you iterate over .VARIABLES and print each, any true variable can be printed correctly with the following rule:
print_variables: $(foreach V,$(.VARIABLES),print-$(V)) .phony_explicit
print-%: .phony_explicit; #echo "$* = \"$($*)\""
.PHONY: .phony_explicit ...
A 0- or 1-line function will still work, but any more will result in Syntax error: Unterminated quote string. Just one multiline function will break the entire print_variables rule. As a workaround, I have added ;\ to each line in my function definitions, but that won't fix existing multiline functions (either via includes from this makefile or via other makefiles including this one.) What can I do? Is there a container of just function variables, or a way to test if a variable is a function definition?
A simple minimal example would be easier to understand; this has nothing to do with .VARIABLES, pattern rules, etc. (and I'm not sure what the point of the .phony_explicit prereq is..)
define F
foo
bar
endef
print: ; echo "F = $(F)"
will show the problem:
echo "F = foo
/bin/sh: 1: Syntax error: Unterminated quoted string
This is because when make sees a variable that contains newlines in a recipe, it assumes that the newlines mean you want the lines of the variable to become lines in the recipe.
First in general you should use single-quotes around strings you send to the shell, unless you need the shell to expand them; it won't help in this situation but in general it's much safer.
There's no way to undo that, really. You have a number of options.
The first is to not use echo but instead use the make function info:
print-F: ; $(info F = "$(F)")
yields:
F = "foo
bar"
Another option is to use subst to replace the newlines with some other value. The new value cannot itself contain explicit newlines, but you can ask the shell to print a newline for you:
# Create a variable containing a single newline
# Note this must contain TWO newlines!
define NL
endef
print-F: printf 'F = "$(subst %,%%,$(subst $(NL),\n,$(F))"\n'
Yields:
printf 'F = "foo\nbar"\n'
F = "foo
bar"
One final option is to convert your makefile to use the .ONESHELL feature, but I assume that's a step too far just to get this debugging output available :).

What is the significance of {*} when used with args like {*}args in tcl

What is the significance of {*} when used with args like {*}args?
For instance: how is using the following in a class method
next a {*}$args b
different from using
next a $args b
It's the argument/list expansion operator. It converts a list into many single words.
It's documented as part of the tcl syntax rules: http://www.tcl.tk/man/tcl/TclCmd/Tcl.htm. It is rule number 5.
For example, say you have a list:
set foo {a b c d}
and you call the command:
bar $foo
The command will be interpreted as:
bar "a b c d"
But if instead you do:
bar {*}$foo
Then the command will be interpreted as:
bar a b c d
You can see the difference for yourself in the interactive shell by putting the command list in front of the invocation:
list next a {*}$args b
list next a $args b
Set args to some value (preferably some list value), run the above, and you'll see what the invocation actually looks like.
{*} splices the element of a list as individual arguments, not the whole list as a single argument.

Use of [list a b c] vs {a b c} when creating a list

What differences are there between creating a list in TCL using:
[list a b c]
vs
{a b c}
I'm by all means not an experienced TCL programmer, but the only difference I have encountered so far is when creating a list of multiple lines the first style requires using line continuation characters like:
[list \
a \
b \
c \
]
where this parses fine:
{
a
b
c
}
Are there any other differences? Which is considered better style or idiomatic?
It would appear that when creating a complex list with nested lists, the 2nd style is the only clean way to go.
The main difference is that using the list command makes it possible to use variables when defining the list. Notice the difference between these two:
% set foo 1
1
% set bar 2
2
% set list1 [list $foo $bar]
1 2
% set list2 {$foo $bar}
$foo $bar
Note that you can also use double quotes if you want:
% set list3 "$foo $bar"
1 2
It's important to note that of the two ways to build a list with variables, only using list is guaranteed to give you a proper list. Using quotes may or may not give you a list, depending on the contents of the variables. This isn't because Tcl is mysterious or random or buggy -- it's simply how tcl quoting works. With list you are asking tcl to construct a list with specific elements, in the others you're creating a string that looks like a list, but whether it can be treated like a list or not depends on the data in the string.
Here's an example where using quotes won't give you a list:
% set foo "{"
{
% set list4 "$foo $bar"
{ 2
% lindex $list4 0
unmatched open brace in list
... whereas using list will give you a proper list:
% set list5 [list $foo $bar]
\{ 2
% lindex $list5 0
{
It's important to know that the backslash appears only when tcl converts the list to a string for the purposes of printing the list -- the backslash isn't in the data, as you can see when you use lindex to fetch the value.
Are there any other differences? Which is considered better style or idiomatic?
In the case where you're after a list with just literals in it, no variable or command expansion or anything like that, using [list a b c] and {a b c} is exactly the same. They compile to identical bytecode in Tcl 8.6 (the push of a literal onto the result stack). There really is no difference at all.
Which is more idiomatic? I don't really know, to be honest. They are both idiomatic, and subject to individual preferences. The difference between them becomes important once you start using variable and command expansion, and then the question quickly ceases to be relevant.
That said, I mostly prefer to use {a b c}. It's more convenient when the literals are longer since I can break things over multiple lines without fussing around with backslash continuations. Other people will disagree with me; for them, the typing of [list…] reminds them strongly of what they intend to do with the data, and that's clearly of some mnemonic value.

Haskell syntax, binding file contents to variables

The main idea of the code is this I load an expression from a file which has only 1 line like 100 + a + (x ^ 2)
I want to load it in a function and check if it's ok (so it is not like )) + 5 2 a **). First I want to say that I know my checks are not covering all type of mistakes but we have to check only for some mistakes. First here is the code
main = do
contents <- readFile "file.txt"
let stringToCheck <- line contents
checkIfProper filter (/=' ') stringToCheck
As you can see I am really new to Haskell and I would like this to work and I would fix it step by step. I think logically it is fine and there are only syntax mistakes that I can't understand.
My first question is at the begining of the main I load from a file.txt this file has only 1 line and I take it in the stringToCheck and then I want to use filter on it to remove all spaces but this doesn't work. If I call the filter on a string I write down at the moment it's fine but when I load it from the file it has problems I guess I don't take the line properly so how to do this?
How do you take a string properly from a file and use it as a variable in other function?
main = do
contents <- readFile "file.txt"
So far everything fine. You've read in the file in IO, and used the do-block syntax with <- correctly. For everything underneath, contents is a String variable that holds the contents of the file.
let stringToCheck <- line contents
Here, you do not need this special assignment syntax: at this point as I said, contents is a "pure" value, so anything you use it for is purely functional and you can use ordinary =. That's always done in let blocks. So...
let stringToCheck = line contents
Well, that doesn't work because there's no line function. There is lines, which splits up a text in a list of strings, each representing a line; to retrieve the first one use
let stringToCheck = head $ lines contents
but if the file only has one line then this is equivalent to simply stringToCheck = contents.
checkIfProper filter (/=' ') stringToCheck
Now, here is suppose you mean checkIfProper (filter (/=' ') stringToCheck): the function checkIfProper takes just a single argument, but if you just write out a composite expression that is misparsed as a couple of different arguments, i.e. checkIfProper filter (/=' ') stringToCheck actually means what in un-curried languages would be written checkIfProper(filter, (/=' '), stringToCheck). I don't think you mean that!
The idiomatic way of writing it in Haskell is
checkIfProper $ filter (/=' ') stringToCheck
The resulting main might still not compile: the result of checkIfProper needs to have type IO () so you can simply append it to a do block as in main, but apparently it has type String -> Bool. Perhaps you want the result printed out, that's done with
print . checkIfProper $ filter (/=' ') stringToCheck
In case you're not familiar with . and $: you can read an expression like
f . g . h $ x + y
as
f (g (h (x + y)))

Haskell call function onload

Good morning fellow programmers!
i'm working on a project using haskell, and i've wanted to know how to run a haskell function without having to type two lines on ghci, for example
ghci filename.hs function
This can only be done doing:
ghci filename.hs
function
????
I'm looking for something like the main () in C,which runs automatically when you compile the program
Is there something like that?
I've been checking the -e option on ghci, but i cant seem to get it to work!
Thank you very much!
Cheers!
You're probably looking for ghc -e instead:
> echo 'foo x y z = x+y*z' > foo.hs % let's make a foo.hs file
> ghc foo.hs -e 'foo 1 2 3' % call the function in foo.hs
=> 7
Also, note that you can also use the :reload command in ghci. Load the file in ghci, edit, type :reload and test again. Also, if this seems too tedious, you can also define a ghci macro which allows you to reload and test your function at the same time:
> :def test \x -> return (":reload\n" ++ x)
> :test foo 1 2 3
=> Ok, modules loaded: Foo.
7
If you're looking to build real programs instead of quickly testing
your functions, then you'd better read the other answers on writing
main functions.
I assume function has the type IO (). Then you can just let main = function, and use for example runhaskell modulename from the command line. As in C, main is a special function.
To clarify a bit, just in case: If your function is a pure one, i.e. one whose type does not invovle IO, you can't really "run it". I guess it's a simplification to say this, but essentially what GHCi does is to call print function. If you want to mimic this, you can use something like main = print function and use runhaskell. This assumes function's type is an instance of Show.