Haskell if else list comprehension - haskell

I'm trying to print out a board in the right way, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
1. . . . . . . . . . . . . . . . . .
2. . . . . . . . . . . . . . . . . .
3. . . . . . . . . . . . . . . . . .
4. . . . . . . . . . . . . . . . . .
5. . . . . . . . . . . . . . . . . .
6. . . . . . . . . . . . . . . . . .
7. . . . . . . . . . . . . . . . . .
8. . . . . . . . . . . . . . . . . .
9. . . . . . . . . . . . . . . . . .
10. . . . . . . . . . . . . . . . . .
11. . . . . . . . . . . . . . . . . .
12. . . . . . . . . . . . . . . . . .
13. . . . . . . . . . . . . . . . . .
14. . . . . . . . . . . . . . . . . .
15. . . . . . . . . . . . . . . . . .
16. . . . . . . . . . . . . . . . . .
17. . . . . . . . . . . . . . . . . .
18. . . . . . . . . . . . . . . . . .
I have the list comprehension here
((concat [(""++show i)++" " | i <- [1..n], i<10])++"\n")
I can get the 9 first numbers correctly, but the problem comes when i would like to put an else statement in my comprehension. I can't seem to know how to do that. So to make my self extra clear, i would like to do the same thing with the double digits but the only difference will be that i want two spaces instead of three between each double digit.

The left hand of the list comprehension accepts any Haskell expression, so we can write the if-then-else in the left hand of the list comprehension and omit the i < 10 condition on the right hand:
concat [(""++show i)++if i < 10 then " " else " " | i &lt- [1..n]]++"\n"
For n = 15, this then produces:
Prelude> concat [(""++show i)++if i < 10 then " " else " " | i <- [1..n]]++"\n"
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 \n"
We can also remove the ""++ part on the left side of the list comprehension, since this is bascially a no operation:
concat [show i ++ if i < 10 then " " else " " | i &lt- [1..n]] ++ "\n"
The above is however not very elegant: if i = 123, then we still will run into trouble. We can calculate the length of the show, calculate 4-l (with l the length), and add this as extra spacing. For instance:
concat [s ++ replicate (4-length s) ' ' | i &lt- [1..n], let s = show i] ++ "\n"
There are also some more dedicated formatting and joining functions, but since the exercise is probably to get familier with lists, I think this is out of scope here.
Like #4castle says, we can use for instance printf:
import Text.Printf(printf)
concatMap (printf "%-4d") [1..n] ++ "\n"

Related

How to share the internet between windows and linux

I have my working pc and a laptop. On the pc I running Windows with domain controller account. When I log on I have access to the Internet. My laptop connected to the same network with Ubuntu operating system. Are there any ways to obtain the Internet access from my laptop? Now i'm simply using remote desktop connection from lap top, but I really need the Internet on my Ubuntu.
IP configuration Windows
HOST NAME . . . . .. . . . . . : MN10384632
DNS . . . . . . . . . . . . . : bs.idest.ru
DNS LOOKUP ORDER. . . . . . . . : bs.idest.ru
idest.ru
LAN - Ethernet:
DNS. . . . . . . . . . . . . . . :
DESCRIPTION . . . . . . . . . . . . : Broadcom NetXtreme Gigabit Ethernet
Dhcp Enabled.. . . . . . . . . .. . . . : disabled
IP-address .. . . . . . . . . . . . . . : 10.1.25.6
Network mask . . . . . . . . . . . . . : 255.255.255.0
Gateway . . .. . . . . . . . . . . .. : 10.1.25.1
DNS. . . . . . . . . . . . . . . . . . . : 10.1.10.4
10.1.10.5
WINS-server . . . . . . . . . . . . . .: 192.168.100.110
GENERAL.DEVICE: enp3s0
GENERAL.TYPE: ethernet
GENERAL.HWADDR: MYMAC
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: CORPORATE
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/83
WIRED-PROPERTIES.CARRIER: on
IP4.ADDRESS[1]: 10.1.25.106/24
IP4.GATEWAY: 10.1.25.1
IP4.DNS[1]: 10.1.10.4
IP4.DNS[2]: 10.1.10.5
Most probably, if you have no rights there is nothing you can do. You need at least to enable routing and NAT on your Windows...

Receiving DUP! when doing ping to Redhat VirtIO interfaces

Wasn't able to find anything related so here goes! :D
Inside Redhat terminal, whenever I do a ping towards a Redhat virtIO Ethernet Adapter interface of Windows it results to DUP! responses.
I've tried pinging either one of those two virtIO interfaces and both yields to duplicate ping responses...
I've read somewhere that it may be due to Windows' RRAS
Though I'm afraid that turning this off would disable also my capability to Remote access via RDP to this Windows machine.
So does anybody encountered this?
Windows interface setup:
Windows IP Configuration
Host Name . . . . . . . . . . . . . . . . : G162401EN
Primary Dns Suffix . . . . . . . . . . . :
Node Type . . . . . . . . . . . . . . . . : Hybrid
IP Routing Enabled . . . . . . . . . . . : No
WINS Proxy Enabled . . . . . . . . . . . : No
Ethernet Adapter EXT-CP2-EL5:
Connection-specific DNS Suffix . . . . . :
Description . . . . . . . . . . . . . . . : Red Hat VirtIO Ethernet Adapter #5
Physical Address . . . . . . . . . . . . : F0:X:Y:Z:A:B
DHCP Enabled . . . . . . . . . . . . . . : No
Autoconfiguration Enabled . . . . . . . . : Yes
IPv4 Address . . . . . . . . . . . . . . : 10.40.90.163(Preferred)
Subnet Mask . . . . . . . . . . . . . . . : 255.255.255.224
Default Gateway . . . . . . . . . . . . . :
NetBIOS over Tcpip . . . . . . . . . . . : Enabled
Ethernet Adapter EXT-CP1-EL4:
Connection-specific DNS Suffix . . . . . :
Description . . . . . . . . . . . . . . . : Red Hat VirtIO Ethernet Adapter #4
Physical Address . . . . . . . . . . . . : F0:D:E:F:G:H
DHCP Enabled . . . . . . . . . . . . . . : No
Autoconfiguration Enabled . . . . . . . . : Yes
IPv4 Address . . . . . . . . . . . . . . : 10.40.90.131(Preferred)
Subnet Mask . . . . . . . . . . . . . . . : 255.255.255.224
Default Gateway . . . . . . . . . . . . . :
NetBIOS over Tcpip . . . . . . . . . . . : Enabled

Forcing MPI to use a specified no. of cores

I am kinda new to MPI, so forgive me if it is a trivial question. I have a quad core CPU. And I want to run an OpenMPI C++ program that uses two processes on a single core. Is there any way to do so? If so, then how? I referred to this link on stack overflow which, most probably, says that there might be a way... If so, then how can I do it?
Since MPI spawns separate processes, the scheduling of processes onto cores is generally/usually performed by your operating system. You can still achieve what you want by manually setting the affinity of the processes to a specific core.
You can do this in Linux using the sched_setaffinity function.
To pin a process to a specific core, you can use the following approach:
#include <mpi.h>
#include <sched.h>
// ...
void pin_to_core(int cpuid) {
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(cpuid,&set);
sched_setaffinity(0,sizeof(cpu_set_t),&set);
}
// ....
int main(int argc, char* argv[]) {
MPI_Init(&argc, &argv);
pin_to_core(0); // pin process to core 0
// ...
MPI_Finalize();
}
Make sure to call this function after you call MPI_Init in your main function and specify the core you want to pin the process to.
The proper way to implement this with Open MPI is to provide a rankfile which tells the library where to place each process. The following rankfile should suffice in your case:
rank 0=localhost slot=0
rank 1=localhost slot=0
It tells Open MPI that both ranks should run on the local host and be bound to OS CPU 0, which is usually the first core. Provide the rankfile in addition to the hostfile or the host list:
$ mpiexec --host localhost --report-bindings --rankfile ./rankfile -np 2 hostname
[cluster:03247] MCW rank 0 bound to socket 0[core 0]: [B . . . . . . .][. . . . . . . .][. . . . . . . .][. . . . . . . .][. . . . . . . .][. . . . . . . .][. . . . . . . .][. . . . . . . .] (slot list 0)
[cluster:03247] MCW rank 1 bound to socket 0[core 0]: [B . . . . . . .][. . . . . . . .][. . . . . . . .][. . . . . . . .][. . . . . . . .][. . . . . . . .][. . . . . . . .][. . . . . . . .] (slot list 0)
cluster
cluster

How can I use semaphore to describe precedence graph?

How can I Use semaphores to describe the synchronization of the eight processes in the general
precedence graph above?
p1 p2 p3 p4 p5 p6 p7 p8
=============================================================================
. . . . . . . .
work . . . . . . .
| . . . . . . .
V(s1, 3) P(s1) . . P(s1) . P(s1) .
x | . . | . | .
work . . | . | .
| . . | . | .
V(s2, 2) P(s2) P(s2) work . | .
x | | | . | .
| work | . work .
| | | . | .
work V(s3, 1) V(s3, 1) P(s3) | .
| x x P(s3) | .
| | | .
| work | .
| | | .
V(s4, 1) V(s4, 1) V(s4, 1) P(s4)
x x x P(s4)
P(s4)
|
work
|
x

haskell read from file

i'm trying to read from file in haskell and process every line
what i have now
main = interact (unlines . (map calculate) . lines)
this let me get every line from input and send it to calculate
now i want to get every line from a file and send it to calculate
this is what i tried
main = do
text <- readFile "input.txt"
let linii = lines text
interact (unlines . (map calculate) . linii)
tell me please how is it correct?
UPDATE below
calculate :: String -> String
calculate s=
case ret of
Left e -> "error: " ++(show e)
Right n -> "answer: " ++ (show n)
where
ret = parse parseInput "" s
main :: IO()
--main = interact (unlines . (map calculate) . lines)
main = do text <- readFile "input.txt"
let linii = lines
putStrLn . unlines $ map calculate linii
Remember that interact takes input from stdin and sends output to stdout. Since you have already read input from a file, you don't need the former. You only need to do the later. You can print a String with putStrln. Putting this all together, change
interact (unlines . (map calculate) . linii)
to
putStrLn . unlines $ map calculate linii

Resources