mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Blogorrhea > jvang

Reply
 
Thread Tools
Old 2018-06-22, 10:52   #45
henryzz
Just call me Henry
 
henryzz's Avatar
 
"David"
Sep 2007
Cambridge (GMT/BST)

173216 Posts
Default

Quote:
Originally Posted by jvang View Post
Having a package manager is really helpful; why doesn't Windows have one of these by default?
Windows sort of does now with the windows store. Not that there is much useful in it.
henryzz is online now   Reply With Quote
Old 2018-06-25, 13:31   #46
Xyzzy
 
Xyzzy's Avatar
 
Aug 2002

32×929 Posts
Default

Quote:
Originally Posted by jvang View Post
I think I might have figured out some basic stuff in Haskell. Calling a function is easy:

λ print "hello, world"
hello, world
Can you tell us why "1.hs" fails to compile?
Code:
m@nuc:~$ cat 1.hs
putStrLn "!"

m@nuc:~$ ghc -Wall -Werror 1.hs
[1 of 1] Compiling Main             ( 1.hs, 1.o )

1.hs:1:1: error:
    Parse error: naked expression at top level
    Perhaps you intended to use TemplateHaskell

m@nuc:~$ cat 2.hs
main :: IO ()
main = putStrLn "!"

m@nuc:~$ ghc -Wall -Werror 2.hs
[1 of 1] Compiling Main             ( 2.hs, 2.o )
Linking 2 …

m@nuc:~$ ./2
!
Xyzzy is offline   Reply With Quote
Old 2018-06-27, 02:10   #47
jvang
veganjoy
 
jvang's Avatar
 
"Joey"
Nov 2015
Middle of Nowhere,AR

6748 Posts
Default

Haskell coming in a bit...

I've added the Debian Administrator's Handbook to my readings, which will guide me through the specifics of my current distribution. My dad wants me to learn Red Hat down the road and get a certification in it, but that's at a later time.

The first 2 chapters discuss the history of Debian's development and it's founding principles as free software. There's also this sort of "case-study" involving a fictional corporation making the switch to Debian on its computers, which helps demonstrate the real-world needs that Debian satisfies. I'm currently reading chapter 4, which concerns the installation of the main distribution.

It has a pretty straightforward way of presenting information and is easy to follow, so that's nice.
jvang is offline   Reply With Quote
Old 2018-06-27, 02:47   #48
jvang
veganjoy
 
jvang's Avatar
 
"Joey"
Nov 2015
Middle of Nowhere,AR

6748 Posts
Default

Quote:
Originally Posted by Xyzzy View Post
Can you tell us why "1.hs" fails to compile?
Code:
m@nuc:~$ cat 1.hs
putStrLn "!"

m@nuc:~$ ghc -Wall -Werror 1.hs
[1 of 1] Compiling Main             ( 1.hs, 1.o )

1.hs:1:1: error:
    Parse error: naked expression at top level
    Perhaps you intended to use TemplateHaskell

m@nuc:~$ cat 2.hs
main :: IO ()
main = putStrLn "!"

m@nuc:~$ ghc -Wall -Werror 2.hs
[1 of 1] Compiling Main             ( 2.hs, 2.o )
Linking 2 …

m@nuc:~$ ./2
!
I know that you need to have modules at the top level for it to work; no clue why

Quote:
Originally Posted by Nick View Post
Can you write a program which takes 2 sorted lists of numbers and merges them into a single sorted list?
For example, if we give it the lists [1,4,7,10] and [2,3,10,11], it would produce the list [1,2,3,4,7,10,10,11].
I can do [1,4,7,10] ++ [2,3,10,11] to get [1,4,7,10,2,3,10,11], but I have zero clue how to sort it. Something on Stack Overflow said to import something, someone was trying to make their own algorithm, and neither made sense... and as far as making a program to do it with two inputs? I have no idea how to that either. The most I can do is have two predetermined lists and ++ them together. I might be able to write in one input, but that's by copypasta-ing some example code from the Haskell book and I won't really know how/why it works
jvang is offline   Reply With Quote
Old 2018-06-27, 08:12   #49
VictordeHolland
 
VictordeHolland's Avatar
 
"Victor de Hollander"
Aug 2011
the Netherlands

49816 Posts
Default

Quote:
Originally Posted by jvang View Post
I can do [1,4,7,10] ++ [2,3,10,11] to get [1,4,7,10,2,3,10,11], but I have zero clue how to sort it. Something on Stack Overflow said to import something, someone was trying to make their own algorithm, and neither made sense... and as far as making a program to do it with two inputs? I have no idea how to that either. The most I can do is have two predetermined lists and ++ them together. I might be able to write in one input, but that's by copypasta-ing some example code from the Haskell book and I won't really know how/why it works
I think you're looking for the Haskell equivalent of the Java function:
Code:
Array.sort(yourarrayname)
Something like this:
Code:
nums = [2,4,3,1,2] :: [Int]
sorted = List.sort nums
according to:
https://rosettacode.org/wiki/Sort_an_integer_array#Haskell
VictordeHolland is offline   Reply With Quote
Old 2018-06-27, 09:58   #50
Nick
 
Nick's Avatar
 
Dec 2012
The Netherlands

2·3·293 Posts
Default

OK, here is a possible solution - can you see how it works?

We create a text file called Merge.hs looking like this:

Code:
module Merge
    where

merge :: [Int]->[Int]->[Int]
merge xs [] = xs
merge [] ys = ys
merge (x:xs) (y:ys) =
  if x < y
    then x:merge xs (y:ys)
    else y:merge (x:xs) ys
We then load the module and use it, for example like this:

Code:
nick@sirius:/u/nick/tech> ghci Merge.hs
GHCi, version 8.0.2: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Merge            ( Merge.hs, interpreted )
Ok, modules loaded: Merge.
*Merge> merge [1,4,7,10] [2,3,10,11]
[1,2,3,4,7,10,10,11]
*Merge>
Nick is online now   Reply With Quote
Old 2018-06-27, 14:00   #51
henryzz
Just call me Henry
 
henryzz's Avatar
 
"David"
Sep 2007
Cambridge (GMT/BST)

173216 Posts
Default

Quote:
Originally Posted by Nick View Post
OK, here is a possible solution - can you see how it works?

We create a text file called Merge.hs looking like this:

Code:
module Merge
    where

merge :: [Int]->[Int]->[Int]
merge xs [] = xs
merge [] ys = ys
merge (x:xs) (y:ys) =
  if x < y
    then x:merge xs (y:ys)
    else y:merge (x:xs) ys
We then load the module and use it, for example like this:

Code:
nick@sirius:/u/nick/tech> ghci Merge.hs
GHCi, version 8.0.2: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Merge            ( Merge.hs, interpreted )
Ok, modules loaded: Merge.
*Merge> merge [1,4,7,10] [2,3,10,11]
[1,2,3,4,7,10,10,11]
*Merge>
@jvang If you could explain to those who don't already know Haskell how this works that would be useful. I can program quite a few languages well enough to understand that sort of code but don't get the Haskell version.
henryzz is online now   Reply With Quote
Old 2018-06-28, 01:50   #52
jvang
veganjoy
 
jvang's Avatar
 
"Joey"
Nov 2015
Middle of Nowhere,AR

1BC16 Posts
Default

Quote:
Originally Posted by henryzz View Post
@jvang If you could explain to those who don't already know Haskell how this works that would be useful. I can program quite a few languages well enough to understand that sort of code but don't get the Haskell version.
I'll explain what I know line-by-line, but I don't know much

Code:
module Merge
    where
I'm really unfamiliar with commands in imperative and functional languages, but I'm guessing where is a generic IO loop, indented to the 2nd level for the loop at the end of the program. Also, Merge is the module that he plans to load for inputs through the GHCi.

Code:
merge :: [Int]->[Int]->[Int]
merge with a lowercase is the lower level module of Merge. Invoking merge while Merge is loaded will execute everything (functions, types, etc.) associated with merge. :: assigns a type, but I have no clue what [Int] -> [Int] -> [Int] is doing In the examples I've seen, I think it should be [Int] -> IO], but I'll have to test it. Nope, didn't work.
Code:
merge xs [] = xs
merge [] ys = ys
I think this is defining variables for the module, but I don't know why it's xs instead of x. Perhaps when dealing with lists as inputs you append s to the end of the variable?

I think the empty brackets are to ensure the List type, but not sure there either. Perhaps they cause the variable to refer only to the input lists.

Code:
merge (x:xs) (y:ys) =
  if x < y
    then x:merge xs (y:ys)
    else y:merge (x:xs) ys
This is where the inputs are executed upon in the program. I don't know what's up with the syntax (x:xs). I think it's referring to each component of the list sequentially. In the if-then-else loop (the else argument is mandatory), I think Nick is using some recursion to reapply part of the program to itself. The module merge has a value now, and each time it is invoked it changes its own value. Not actually sure what operations are occurring.

Experimented with some things; [Int] -> IO does not work, and I also tried replacing Int with Num since I thought it was a valid type in this case (it's not). Additionally, the order of merge [] ys = ys as opposed to merge ys [] = ys does not seem to matter (returns a "Pattern match is redundant" error for either; it's a non-fatal error and runs fine, but not sure how to remedy it to have no error.).

That's about all I can explain. Everything else is unknown to me
jvang is offline   Reply With Quote
Old 2018-06-28, 01:54   #53
jvang
veganjoy
 
jvang's Avatar
 
"Joey"
Nov 2015
Middle of Nowhere,AR

22·3·37 Posts
Default

As far as Linux is concerned, there's a couple of neat things I've learned for installation. Firstly, apparently you can choose to give root permissions to the normal user account and not have a root account, using sudo to invoke those permissions. And this whole partitioning thing is complicated

The book covers everything in the installation process, even things like how to navigate the BIOS with your keyboard
jvang is offline   Reply With Quote
Old 2018-06-28, 02:06   #54
Xyzzy
 
Xyzzy's Avatar
 
Aug 2002

32·929 Posts
Default

Quote:
Originally Posted by jvang View Post
Firstly, apparently you can choose to give root permissions to the normal user account and not have a root account, using sudo to invoke those permissions.
https://en.wikipedia.org/wiki/Wheel_(computing)


Last fiddled with by Uncwilly on 2018-06-28 at 20:54 Reason: Fixed url )
Xyzzy is offline   Reply With Quote
Old 2018-06-28, 08:48   #55
Nick
 
Nick's Avatar
 
Dec 2012
The Netherlands

175810 Posts
Default

In Haskell, we use square brackets to indicate a list.
So if you understand what these mean:
Code:
[1,2,3]
[1,2]
[1]
... then you can probably work out what this means:
Code:
[]
The colon (sometimes referred to in books as "cons") constructs a new list by adding the item given on its left to the start of the list given on its right. For example:
Code:
Prelude> 1:[2,3]
[1,2,3]
Of course, you can use several colons in a row:
Code:
Prelude> 1:2:3:[]
[1,2,3]
A double colon is used to declare the type of something.
In other words, we are not saying what its value is, yet, just what sort of value it will take.
For example, "Int" means an integer, and "[Int]" means a list of integers.
We use "->" to show that we are declaring a function, giving the type of its parameter on the left, and the type of its result (return value) on the right.
For example,
Code:
double :: Int -> Int
would mean that double is going to be a function taking an integer and returning an integer as well.

The line
Code:
merge :: [Int]->[Int]->[Int]
means that merge is a function taking two lists of integers and returning a list of integers.
(The notation is logical because, for example, merge [1,4,7,10] will have type [Int] -> [Int] .)
Code:
merge xs [] = xs
merge [] ys = ys
merge (x:xs) (y:ys) =
  if x < y
    then x:merge xs (y:ys)
    else y:merge (x:xs) ys
Here we define what the merge function does, giving 3 cases.
The first line says what the result is if the second parameter is the empty list (just return the first one).
The second line says what to do if instead the first parameter is the empty list (return the second one).
So the computer only reaches the rest if both the lists it has been given are non-empty,
and we can therefore split off their first elements: in the first list, we call the first element x and all the rest xs;
in the second list, we call the first element y and all the rest ys.
(So xs and ys are lists which may or may not be empty.)
If x<y then the first item in the list we return must be x, followed by the result of merging xs with y:ys
(i.e. the rest of the first list with the whole of the second list).
Otherwise the first item in the list we return must be y, followed by the result of merging x:xs with ys
(i.e. the whole of the first list with the rest of the second list).
Nick is online now   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Musings of someone learning airsquirrels Math 20 2018-12-29 18:15
Online language-learning course kladner Lounge 8 2013-04-18 03:08
Learning Python - Course from Google Jeff Gilchrist Programming 3 2012-01-15 00:29
flowcharts, self-learning jasong jasong 6 2007-12-07 14:06
Learning About RAM the Hard Way Longshot Hardware 5 2005-05-21 16:40

All times are UTC. The time now is 13:43.


Sun Dec 5 13:43:05 UTC 2021 up 135 days, 8:12, 1 user, load averages: 1.67, 1.35, 1.45

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.