First off I am using ghci under ubuntu 11.10 to run the haskell code. 2nd this is my first attempts at haskell. Third, how might I load a file into ghci and where does it need to be located and what should its extension be? I know “:l “file.haskelxtnsn”” is how to load a file, but that’s my best guess so far.
Seeing as I can do the above, how does this code look for merging two list of possibly infinite size in ascending order. (I can’t put this in the prelude> prompt because of indentation???) Given [1, 2, 3] and [4, 5, 6] I should get [1, 2, 3, 4, 5, 6], and I think the usage would be “take 10 (merge listx listy)”
let merge x y = (min (head x) (head y)) :
case (min (head x) (head y)) of
head x -> merge (drop 1 x) y
head y -> merge x (drop 1 y)
psuedo:
- output the min of the heads of the lists
- if the first lists head was output call merge with the rest of the first list and the second
- else call merge with the first list and the rest of the second list
:cdin ghci to change directory, you can also supply a path to the:load(:lfor short) command.Your logic is correct, although maybe I’d write it a bit differently (hopefully you know about and where clause and defining a function as a series of equations):
letin front of definitions, which is different from thelet ... in ...expression. This is rather confusing so I suggest you just put your code in a file and load it in ghci.:operator, so some of you parenthesis is not needed. We usually try to minimize the number of parenthesis to make the code more concise, but don’t be over zealous about it.head xinside a pattern but you can dox:xs(Although I didn’t here). Callingheadandminmultiple times looks redundant, andy ou can also substitutedrop 1withtail.