Jump to content

Haskell


jaberwacky
 Share

Recommended Posts

This thread is intended to document my progress and small epiphanies as I learn this strange and wonderful language.

So I'm following this tutorial: https://en.wikibooks.org/wiki/Yet_Another_Haskell_Tutorial which has been awesome so far

I came across the following exercise:

Define a recursive function mult that takes two positive integers a and b and returns a*b, but only uses addition (i.e., no fair just using multiplication). Begin by making a mathematical definition in the style of the previous exercise and the rest of this section.

And I solved it!  I know.  I know.  Big whoop.  It's great for me as someone who started out as a non-math type.  The solution I came up with isn't exactly like the official solution though, but it seems to work.

rec a 0 = 0
rec a b = 
    a + (rec a (b - 1))

 

Edited by jaberwacky
Link to comment
Share on other sites

Honestly being such a functional programming newb, I don't know exactly.  I guess mostly because there are a lot of tutorials and other resources based on Haskell.  I also see the syntax as being the most simple at this point.  I could very well change from Haskell to something else as I learn.

Link to comment
Share on other sites

FINALLY! After much hair pulling, forehead smashing, and crying I finally hacked up this haskell program that takes a sentence that the user enters and then reverses the inner letters of each word.  For instance this sentence, "Why did you pick Haskell as the next language to learn" becomes this, "Why did you pcik Hleksal as the nxet lgaugnae to lraen".

I'm so relieved.  I can sleep now.

module Main

where

import System.IO

main = do   
    hSetBuffering stdin LineBuffering

    main_rec
        
main_rec = do
    putStrLn "Enter a sentence: "
    
    sentence <- getLine
    
    if toLower sentence == "exit" then do
        print "Bye!"
    else do     
        let word_list = words sentence
        
        let scrambled = map thaScrambla word_list
    
        print scrambled 
        
        main_rec
    
-- Designed to scramble your internals.
thaScrambla :: [Char] -> [Char] 
thaScrambla word 
    | length word > 3 = [head word] ++ (reverse $ tail $ init word) ++ [last word]
    | otherwise       = word

toLower :: [Char] -> [Char] 
toLower word 
    | word == "Exit" = "exit"
    | otherwise      = word
module Main

where

import System.IO

main = do   
    hSetBuffering stdin LineBuffering

    main_rec
        
main_rec = do
    putStrLn "Enter a sentence: "
    
    sentence <- getLine
    
    if toLower sentence == "exit" then do
        print "Bye!"
    else do     
        let word_list = words sentence
        
        let scrambled = map thaScrambla word_list
    
        print scrambled 
        
        main_rec
    

thaScrambla :: [Char] -> [Char] 
thaScrambla word = do
    if length word > 3 then do
        [head word] ++ (reverse $ tail $ init word) ++ [last word]
    else word

toLower :: [Char] -> [Char] 
toLower "Exit" = "exit"
toLower x = x
module Main

where

import System.IO

main = do   
    hSetBuffering stdin LineBuffering
    
    putStrLn "Enter a sentence: "
    
    sentence <- getLine
    
    let word_list = words sentence
    
    let scrambled = map thaScrambla word_list
    
    print scrambled
    
thaScrambla word = do
    if length word > 3 then do
        [head word] ++ reverse (tail (init word)) ++ [last word]
    else word

Bizarro World: FLLANIY Aetfr mcuh hiar pnillug faeherod snihsamg and cniyrg I fllaniy hekcad up tihs hleksal pargorm taht tekas a scnetnee taht the uesr eretns and tehn resreves the iennr lrettes of ecah wrod   

Edited by jaberwacky
better code
Link to comment
Share on other sites

Bizarro World: FLLANIY Aetfr mcuh hiar pnillug faeherod snihsamg and cniyrg I fllaniy hekcad up tihs hleksal pargorm taht tekas a scnetnee taht the uesr eretns and tehn resreves the iennr lrettes of ecah wrod   

Translato: Bizarre World: FINALLY After much hair pulling forehead smashing and crying I finally ?????? up this haskell program that takes a sentence that the user enters and then reverses the inner letters of each word

 

I used a proprietary language which is used in a proprietary system to translate it back into English... That system is homo sapian and the lagauage is homo sapianian I think... TD :P

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

How's Haskell for you so far (apart from the hair pulling)?

I wouldn't take my opinion as fact.  I approach Haskell and functional programming from a place of little to no mathematical ability.  It's been a hair pulling experience.  Since my last post I haven't done much Haskell particular stuff.  Just reading about general functional programming concepts. 

In my experience, I have been slowly arriving to a functional style in my imperative scripts.  So I have been picking up on the concepts pretty well as long as they're explained from a practical view point.  I don't do very well with the category theory stuff.  Supposedly that stuff isn't necessary to program functionally though.

Edited by jaberwacky
Link to comment
Share on other sites

It just occurred to me that Haskell is the first language that I've tried to learn where I did not give one thought to syntax highlighting.

Edit:

Actually, that's not entirely true.  I did try to set up SciTE to use the Haskell syntax highlighter, but the option isn't there in the language drop down.  Still, the point stands, it's not that big of a deal for some reason.  Just a thought.

Edited by jaberwacky
Link to comment
Share on other sites

Wow! F# is multi-platform, supports android & no spaghetti code! Just what I need! Thanks Manadar & Jaber for the link, TD :thumbsup:

Edited by TheDcoder
blank line at the start

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...